Skip to main content

Deploying 3-Tier Application on Docker | Containerizing NodeJs, Mongo Express, MongoDB

Updated 2 min readMarkdown
Deploying 3-Tier Application on Docker | Containerizing NodeJs, Mongo Express, MongoDB

https://gitlab.com/nanuchi/developing-with-docker

To start the application

Step 1: Create a docker network

docker network create mongo-network

Step 2: start MongoDB

docker run -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=password --name mongodb --net mongo-network mongo

Step 3: start mongo-express

docker run -d -p 8081:8081 -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin -e ME_CONFIG_MONGODB_ADMINPASSWORD=password --net mongo-network --name mongo-express -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express

NOTE: creating docker-network in optional. You can start both containers in a default network. In this case, just emit --net flag in docker run command

Step 4: open mongo-express from browser

http://ip address :8081

user-account db userscollection

  1. Create a Dockerfile with the content you provided:

     FROM node:13-alpine
    
     ENV MONGO_DB_USERNAME=admin \
         MONGO_DB_PWD=password
    
     RUN mkdir -p /home/app
    
     COPY ./app /home/app
    
     WORKDIR /home/app
    
     RUN npm install
     EXPOSE 3000
    
     CMD ["node", "server.js"]
    
  2. Save the Dockerfile in a directory along with your application code. Let's assume the directory is named "my-app".

  3. Open a terminal or command prompt and navigate to the directory containing the Dockerfile and your application code ("my-app" directory).

  4. Build the Docker image using the following command:

     sudo docker build -t my-app-image .
    

    This command will build the Docker image with the tag "my-app-image". The dot (.) at the end specifies the build context as the current directory.

  5. Once the image is successfully built, you can run a container from it and connect it to the "mongo-network" network using the following command:

     docker run -d --name my-app-container --net mongo-network -p 3000:3000 my-app-image
    

    This command starts a container named "my-app-container" from the "my-app-image" image, connects it to the "mongo-network" network, and maps port 3000 of the container to port 3000 on the host machine.

About the author

  • Hey there! I'm currently working as an Associate DevOps Engineer, and I'm diving into popular DevOps tools like Azure Devops,Linux, Docker, Kubernetes,Terraform and Ansible. I'm also on the learning track with AWS certifications to amp up my cloud game. If you're into tech collaborations and exploring new horizons, let's connect!

No comments yet. Be the first to comment.

More from this blog