Goglides Dev 🌱

Prason Pandey
Prason Pandey

Posted on

How To Containerize Simple Node.js Web Applications

It's no secret that Docker is one of the most popular containerization technologies available today. Docker lets you package an application along with its dependencies into an isolated, compact container. This process is known as containerization.

Why containerizing an app is important

Image description

When running your application, a small change in a package/library can cause errors or change functionality. With the help of Docker, you can containerize the application which allows the application to execute the application the same way in every computer or server with no errors or functionality changes.

Using Docker, you can make an image of a code base and its packages, and then run a container based on that image. Additionally, you can share your images with others.

Dockerfile

An image can be created automatically by Docker by reading instructions in a Dockerfile. This is a text document that contains all the commands that could be used to assemble an image. For more details here.

Goal

In this blog, You will be learning how to containerize simple Node.js API using Docker.

Prerequisites

  • Basic understanding of Python and Flask
  • Basic understanding of Linux
  • A code editor
  • Basic Understanding of Docker

Application Structure

Throughout this blog, we will be using basic code from my github, which is a simple node.js application. You can clone the repository from here.

Application Structure looks like this:

└── resume-project
    β”œβ”€β”€ controllers
    β”‚   └── authController.js
    β”œβ”€β”€ middlewares
    β”‚   └── verifyToken.js
    β”œβ”€β”€ model
    β”‚   └── User.js
    β”œβ”€β”€ package.json
    β”œβ”€β”€ package-lock.json
    β”œβ”€β”€ README.md
    β”œβ”€β”€ routes
    β”‚   β”œβ”€β”€ auth.js
    β”‚   └── posts.js
    β”œβ”€β”€ server.js
    └── validation.js

Enter fullscreen mode Exit fullscreen mode

Creating a Docker file

Dockerfiles are used to create images of our application, which can be run in any Docker installed server or environment.

FROM node

WORKDIR /app
COPY . /app

RUN npm install

ENTRYPOINT ["node"]
CMD ["app.js"]

Enter fullscreen mode Exit fullscreen mode

The above Dockerfile contains the commands for building our image.

Building Image

Now, It's time to make an image from the docker file. To build an image with the name β€˜mysite’, you can use the following command.

docker build . -t mysite
Enter fullscreen mode Exit fullscreen mode

Image description

We have successfully built an image from the docker file. We can view our images with the following command.

docker images
Enter fullscreen mode Exit fullscreen mode

Image description

Running Container

We can easily run our container from our image with the following command.

Docker run -p 8000:3000 -d mysite --name mysite
Enter fullscreen mode Exit fullscreen mode

Docker run command is used to run any container, p flag is being used to map the external port(8000) to the port inside the container(3000), d flag is being used to run the container as a background process.

You can view all the running containers using

docker containers ls

Image description

Since the container is running, you can access the website on 0.0.0.0:8000.

This is how you can containerize simple node js application. This much for this blog, If you like it, Follow me for more blogs.

Top comments (0)