Node.js & MongoDB: A Beginner's Guide to Building Modern Web Applications
Multiple containerization using dockercompse for Nodejs with Mongodb

Node.js & MongoDB: A Beginner's Guide to Building Modern Web Applications

Dockerfile for Node.js Application

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Docker can build images automatically by reading the instructions from a Dockerfile.

Here's a simple example of a Dockerfile for a Node.js application:

# Use an official Node runtime as a parent image

FROM node:14

# Set the working directory in the container

WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app

COPY . .

# Install any needed packages specified in package.json

RUN npm install

# Make port 3000 available to the world outside this container

EXPOSE 3000

# Run app.js when the container launches

CMD ["node", "app.js"]

This Dockerfile does the following:

  • Starts from a base image with Node.js pre-installed.
  • Sets the working directory inside the container.
  • Copies the application's files into the container.
  • Installs the application's dependencies defined in package.json.
  • Exposes port 3000 to the Docker host, so the application can be accessed from outside.
  • Specifies the command to run the application.

Docker Compose and docker-compose.yml

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

A docker-compose.yml for a Node.js application with MongoDB might look like this:

Please check the docker-compose.yml in githubcode ->Docker-compose.yml


This docker-compose.yml file does the following:

  • Defines two services: app (our Node.js application) and mongo (our MongoDB database).
  • The app service is built from the Dockerfile in the current directory.
  • It maps port 3000 on the host to port 3000 on the container, allowing you to access the application.
  • It specifies a dependency on the mongo service, ensuring MongoDB is up before the application starts.
  • The mongo service uses the official MongoDB image.
  • Maps port 27017 on the host to the same port on the MongoDB container, allowing direct MongoDB access if needed.
  • Persists MongoDB data using a named volume (mongo-data).

Steps to Run Your Application

  1. Create a Dockerfile: For your Node.js application as described above.
  2. Create a docker-compose.yml file: As shown above to define both your application and MongoDB service.
  3. Build and Run with Docker Compose:

Docker Compose is particularly useful for development and testing environments, allowing you to easily link your application with services like databases without manual configuration or having to install these services on your development machine.


Nodejs Application on RestApi with Mongodb database with Handsonlab


Please check with github source code: https://github.com/jaganrajagopal/DockercomposeNodejsMongodb.git


要查看或添加评论,请登录

Jagan Rajagopal AWS Certified Solution Associate ,Aws Coach Jagan ,Azure ,Terraform的更多文章

社区洞察

其他会员也浏览了