Nodejsmongodb

How to create the docker image for nodejs with mongodb database using docker compose file

Creating a Docker image for a Node.js application with MongoDB involves writing a Dockerfile for the Node.js app and a docker-compose.yml file to define and run multi-container Docker applications. Below are the steps and sample configurations for setting up your Node.js application and MongoDB with Docker.

Step 1: Create a Dockerfile for Node.js Application

  1. In the root of your Node.js project, create a file named Dockerfile.
  2. Add the following content to the Dockerfile:
# Use an official Node runtime as a parent image
FROM node:14

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install any needed packages
RUN npm install

# Bundle app source inside Docker image
COPY . .

# Make port available to the world outside this container
EXPOSE 3000

# Define environment variable
ENV MONGO_DB_URI=mongodb://mongo:27017/bookstore

# Run app when the container launches
CMD ["node", "app.js"]
  1. This Dockerfile sets up a Node.js environment, copies your application code into the Docker image, installs dependencies, and sets the command to start your app.

Step 2: Create a docker-compose.yml File

  1. In the root of your project, create a file named docker-compose.yml.
  2. Add the following content to the docker-compose.yml file:
version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    links:
      - mongo
    depends_on:
      - mongo
  mongo:
    image: mongo
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db
volumes:
  mongo-data:
  1. This configuration defines two services: app (your Node.js application) and mongo (the MongoDB database). It also sets up a volume for MongoDB data persistence.

Step 3: Building and Running Your Containers

  1. Navigate to your project directory (where your Dockerfile and docker-compose.yml are located) in the terminal.
  2. Run the following command to build and start your containers:
docker-compose up --build
  1. This command builds the Docker image for your Node.js application and starts the containers as defined in docker-compose.yml.

Step 4: Accessing Your Application

  • After starting the containers, your Node.js application should be accessible at http://localhost:3000.
  • MongoDB is running in its container and is accessible to your Node.js application via the service name mongo defined in docker-compose.yml.

Please refer How to creathe Nodejs application with mongodb , Click Here

Please find the github source code: https://github.com/jaganrajagopal/Nodejsmongodb.git

Notes

  • Make sure you have Docker and Docker Compose installed on your machine.
  • Update the ENV MONGO_DB_URI environment variable in the Dockerfile with the correct connection string for your MongoDB setup.
  • You might need to modify the Docker configurations depending on your specific application setup and requirements.
  • This setup is for development purposes. For production, consider using Docker secrets for sensitive data, optimizing your Docker images, and other best practices.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *