How to Dockerize a Node.js Project

How to Dockerize a Node.js Project

Image for post

docker-nodejs

As a Node.js developer, chances are you have at least heard of Docker. If you are new to Docker, this article will get up to speed.

If you are reading this article, I assume you know what docker is and have worked with Node.js before. If you are new to docker, you can get yourself up to speed here and if you are new to Node.js, you can go through this article to get started.

Technologies To Be Used:

Before we Begin, you need to have the following Installed:

Let’s Shoot!

The first thing we will do is create a simple hello-world Node.js application.

Initialize a new Express project, open your terminal or command prompt and execute the following commands:

$ touch index.js</span>

As I said earlier, we will develop a simple hello-world application, nothing serious.

Open your index.js file and paste the following code:

Now that we have the application setup, if you run the application with the command node index.js, you should see the following on the console:

Running on [http://0.0.0.0:8080](http://0.0.0.0:8080)</span>

Now, we can proceed to dockerizing our hello-world application.

The first step in dockerizing a project is to create the Dockerfile. A Dockerfile specifies the resources that will be required in your application container when it is executed.

Run the following command in your terminal to create a Dockerfile for your project.

$ touch Dockerfile</span>

Now, copy the paste following code in your Dockerfile:

Explanation:

The first line sets the application’s base image which in our case is node:10

The next instruction sets the application’s working directory (if a WORKDIR is not specified, Docker will create one by default).

The next instruction copies package.json and package-lock.json from our local project folder to our application Docker container.

The next instruction installs all our application dependencies specified in the package.json.

The next instruction specifies the port on which application docker will be exposed/accessed.

Lastly, CMD runs the command to start our application which in our case is node index.js. Note that there should only be one CMD instruction in each Dockerfile. If you include more than one, only the last will take effect.

Next, we will create a .dockerignore file. It is very similar to .gitignore . It specifies which files and directories should not be copied into our docker container.

Run the following command in your terminal to create a .dockerignore for your project.

$ touch _._dockerignore</span>

Now, copy the paste following code in your .dockerignore:

Next, we need to build our application docker image using the docker build. We will include a -t flag with docker build to allow us tag the image with any name we want. In this case, I will use “my-docker-app” as the name but you can use any name you prefer.

$ docker build -t my-docker-app .</span>

Now if you run docker images you should see your application image in the list of images.

$ docker images

# Example
REPOSITORY                      TAG        ID              CREATED
node                            10         1934b0b038d1    5 days ago
my-docker-app    latest     d64d3505b0d2    1 minute ago</span>

Now run the following command to run your docker image we just built

$ docker run -p 8000:8080 -d my-docker-app</span>

Explanation:

The docker run command starts the docker image. The “-d” tag was added to leave the image running in the background. The “-p” tag was added to redirect the 8080 which is a public port on your system to 8000 which is a private port inside our docker container.

At this point, your application should be up and running on docker. On your browser, navigate to localhost:8000 to test the dockerized application.

You can also print out the application logs using the following command:

# Get container ID
$ docker ps

# Print app output
$ docker logs <container id>

# output
Running on [http://localhost:8000](http://localhost:8000)</span>

So, that’s basically all about setting up a Node.js application on docker, I hope this article helped you in one way or the other.

You can contact me on twitter, GitHub and Instagram. Enjoy!!!.