Setup Node Js Express MongoDB in Docker

In this article, we will look at how to set up a development environment using Node.js, MongoDB and Mongo Express On Docker. This setup will allow developers to create, test, and deploy their applications with ease and consistency.

Setup Node Express and MongoDB in Docker

Dockerfile



FROM node:latest
MAINTAINER Ridoy
ARG user
ARG uid
RUN mkdir -p /var/www

COPY package.json /var/www/package.json

WORKDIR /var/www

RUN npm install

EXPOSE 3000

CMD ["npm", "run","serve"]

docker-compose.yml

version: "3.7"
services:
  app:
    build:
      args:
        user: Ridoy
        uid: 1000
      context: .
      dockerfile: Dockerfile
    image: node-mongo
    container_name: node-mongo-app
    restart: unless-stopped
    working_dir: /var/www
    ports:
      - ${APP_PORT}:3000
    volumes:
      - ./:/var/www:cached
    links:
      - mongo
    networks:
      - node-mongo-network
    depends_on:
      - mongo
  mongo:
    image: mongo
    container_name: node-mongo
    volumes:
      - ./data:/data/db
    ports:
      - ${MONGODB_PORT}:27017
    networks:
      - node-mongo-network
  mongo-express:
    container_name: mongo-express
    image: mongo-express
    depends_on:
      - mongo
    ports:
      - ${MONGO_EXPRESS_PORT}:8081
    networks:
      - node-mongo-network
networks:
  node-mongo-network:
    driver: bridge

env

APP_PORT=3000

DB_URI=mongodb://mongo:27017/job_portal

MONGODB_PORT=2222

MONGO_EXPRESS_PORT=4444

Run on the background with command:

sudo docker-compose up -d --build 

Stopping containers

docker-compose down

Navigate to http://localhost:3000 in your browser.

Mongo Express http://localhost:4444 in your browser.

Example on github: https://github.com/RidoySarker/Docker-Compose-Nodejs-Express-MongoDB

In conclusion, setting up a development environment using Node.js, MongoDB, and Docker is a simple and straightforward process. This setup will allow developers to create, test, and deploy their applications with ease and consistency.

About Tech Flow Zone

Leave a Reply

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