Day 17: Deploying To-do App with the help of Dockerfile

Day 17: Deploying To-do App with the help of Dockerfile

This is the Day 17 of the #90DaysOfDevOps challenge in which we are deploying the To-Do App in the Docker container with the help of Dockerfile.

But what is Dockerfile?

Dockerfile :

A Dockerfile is a text file document that contains all the commands to build an image that is provided by the user in the Dockerfile. Most companies use their own images to deploy any kind of application onto the production server. With the help of Dockerfile, we can write the set of instructions to pull our own image from the Docker Hub (where all images are stored) with the set of commands that will automatically run while running the container.

Steps to Deploy the To-Do App on the server :

1) Launch an Ubuntu 18.04 instance on the AWS cloud and install Docker on the machine. You can see the installation process of Docker in this blog. (https://amitmaurya.hashnode.dev/day-16-getting-started-with-docker)

2) Now clone the To-Do Project project (https://github.com/LondheShubham153/node-todo-cicd.git) into your Ubuntu system.

3) Create the Dockerfile under the node-todo-cicd-Project. Put the exact same name as the "Dockerfile" name.

vi Dockerfile
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
RUN npm run test
EXPOSE 8000
CMD ["node","app.js"]

Now, let's understand these all keywords of Dockerfile.

  • FROM - This is the start of every Dockerfile where FROM is used to build the layer image of the node.js image. To know the node js latest version you have to go to Docker Hub and search Node you will get the official image of Node and select any tags then it will open the GitHub page of that Dockerfile, you'll get the latest image version name. If you want to run "npm" command you have to use the node base layer image.
  • MAINTAINER - This is optional you can add this to know who is going to build this image.

  • WORKDIR - It is used to set the directory.

  • COPY - It copies the (.) file from the source directory to the (/app) directory.

  • RUN - By using RUN we can give a set of commands that will automatically run when the container will started. NPM is the node package manager which is the world's largest software repository.

  • EXPOSE - It is used to give the port number.

  • CMD - To execute the container we have to give the default parameters.

    Remember one thing in one Dockerfile only one CMD will be present if you use more than one CMD the last one will only come into effect.

To build the given image in Dockerfile execute the docker command.

sudo docker build -t todo-deploy-app:1 .

This will build the image with the name of the todo-app of version 1 at the present directory (.) -t is the target image. If you are doing all these tasks on a normal user execute with "sudo" command that will give the root privilege.

Here, you can see the last two lines which means successfully the target image has been created. You can verify whether the image is created or not by executing docker images .

Now, the image is created. We have to run the container now to see the to-do-project app execute the command.

$ docker run -d --name todo-container -p 8000:8000 todo-deploy-project:1

-d option is detach mode that is used to run the container in the background and -p is the port number 8000:8000 (first: second). The first port number is for Docker Host and the second is for Docker Container. todo-deploy-project:1 is the image name. --name is used to put the container name that you want.

By docker ps you can see your running container.

To access this container go to your web browser and copy the instance <public IP>:8000. 8000 is the port number where you can access your to-do app.

You successfully deployed your Node To-Do app on the server with the help of Dockerfile. That's it for the Day 17 task. And yes !! After all, practicing this remember to stop the instance when you stop the instance your container will also stop.

Follow me on Hashnode for more Linux and DevOps blogs.

Connect with me on Twitter !!!

THANK YOU :)

HAPPY LEARNING :)