Sample application using Docker

Welcome!!

In this blog, we will be working with a simple to-do list application that is running in node js. If you don’t know node js, don't worry. Real-time experience with Node JS or JavaScript is not needed.

“To do” application that we are going to use in this blog.

Get Started

Before we use the application, we should get the application into our system. You can either clone the project or you can download the zip folder of the application here. Use your favorite editor to open the application. In this blog, we will be using Visual studio code, but you can use any editor you like. You should be able to see two folders named spec and src along with the package.json file.

“To do” application in Visual studio code.

Building an image for the “To do” application

To build an image we should have a Dockerfile that is responsible to create images. Docker file is nothing but a file that has instructions for an image to get created.

1. Create a file named Dockerfile in the “App” folder. Make sure you don't add any extensions like .txt as some editors give default extensions for such files, which would result in an error. Inside the Dockerfile add the following contents.

# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

2. Now open the terminal and go to the app folder which has Dockerfile. Now it's time to build the image using the docker build command.

docker build -t getting-started .

As soon as we run this command, Dockerfile will start executing and you can also notice that many “layers” were downloaded. That's because in the first line of Dockerfile we have instructed to use FROM node:12-alpine since we don't have Node in our system it has to be downloaded from the docker registry which is the docker hub. Once the image was downloaded, we used COPY . . to copy our application. RUN yarn install --production we used yarn in this command to install all the dependencies needed for our application. The CMD command specifies the default command to run when starting a container from this image.

3. Now the image has been created. Now let's use docker run the command to run our application. Read this to know more about the commands and their use.

docker run -dp 3000:3000 getting-started
  • d flag indicates that the container should run in detached mode. -p maps the host’s port with the container’s port. In this case, it's 3000:3000. At last, getting-started is the name of the image that we have created above. Without the port mapping, we wouldn’t be able to access the application.

Read Getting started with Docker to know more about commands

4. After running the docker run command, open your browser and type http://localhost:3000/. You should see the “To do” application running.

running application

Now you can add some items to check if the application is working correctly or not. At this point, you should have a running to-do list manager with a few items, all built by you.

Let’s open the Docker dashboard. You should be able to see the newly running container in your system.

container that is running in the docker dashboard.

Read this to know more about the Docker dashboard.

Conclusion

In this blog, we learned the basics of how to create an image and used Dockerfile to create an image. After the image has been created we used the docker run command and started the container from the image and saw the running application.

Related articles: