Skip to main content

Command Palette

Search for a command to run...

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

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

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!

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.

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.