Deploying Node.js and MongoDB Application with Docker Containers.
Here we will see how to host a node.js + mongodb website through docker container.
Introduction to Docker
Docker is the world’s leading software container platform. It is a tool designed to make it easier to create, deploy and run the application by using containers. Docker eases the process of deployment of an application and efficient by resolving a lot of issues related to deployment.
Why Containers?
Containers resolve the problem of a code working on one operating system/computing environment and not working on another. Reasons for not working could be lack of dependencies or delayed updates of software or dependency across all the operating systems.
Docker compose
Basically, the Compose file maps the docker run command(s) into a file and it is so convenient as we do not have to type all the parameters to pass to the docker run command. We can declaratively do that in the Compose file.
Creating a docker-compose.yml
Now let’s create a docker-compose.yml file in the same directory in which our other files are present as in our case all the files are present in dwebsite/ directory .
We will define our services/containers inside this file. When creating a docker-compose file, the .yml extension is a must.
docker-compose.yml file :
Breaking down the above code:
=>This compose file defines two services: node and mongo.
=>build field is where we specify the path to dockerfile to create the image
=>Our second service is MongoDB but this time instead of building our own mongo image, we simply pull down the standard mongo image from the Docker Hub registry.
=>As information in a DataBase is non-volatile, we need persistent storage. So, for persistent storage we created an docker volume mongodb mounted at /data/db
=>Finally, we use links command to link both the services.
This way the MongoDB service is reachable from the node service.
Dockerfile :
A Dockerfile is a text document that contains all the commands(set of instructions) that are executed to build an image.
1).In the Dockerfile, we are creating a working directory, WORKDIR /app to instruct Docker to use this path as the default location for all subsequent commands. This way we do not have to type out full file paths but can use relative paths based on the working directory.
2). Before we run npm install, we need to get our package.json and package-lock.json files into our images using COPY command. We copy the package.json and package-lock.json files into our working directory /app.
3). Once we have our package.json files inside the image, we can use the RUN command to execute the command npm install. This works exactly the same as if we were running npm install locally on our machine, but this time these Node modules will be installed into the node_modules directory inside our image.
4). After RUN npm install, we have an image that is based on node version and we have installed our dependencies.
5). COPY . . adds our source code into the image. It takes all the files located in the current directory and copies them into the image.
6).Now, we want to tell Docker what command we want to run when our image is run inside of a container via -
CMD [ "node", "app.js" ].
To start the container run this command:
“docker-compose up -d --build”
Note that we need to use --build if any changes have been made to our Node code (such as views/index.ejs or index.js) since it tells the compose to rebuild the image for our node container.
Now go to the browser and type the url.→
“ localhost:port_no ”
Here is our website→
if we click on ‘contact us’ It will take us to our contact us page.
The filled details will get stored in our mongodb database.
To tear down our containers, we need to use
“docker-compose down”
Now our task is done.