# How to Dockerize a Node.js Project

<span class="r"></span>

# How to Dockerize a Node.js Project


<noscript><img alt="Image for post" class="s t u bx ai" src="https://miro.medium.com/max/2708/1*gX-N6cHK9p715OUKmNiX5g.jpeg" width="1354" height="604" srcSet="https://miro.medium.com/max/552/1*gX-N6cHK9p715OUKmNiX5g.jpeg 276w, https://miro.medium.com/max/1104/1*gX-N6cHK9p715OUKmNiX5g.jpeg 552w, https://miro.medium.com/max/1280/1*gX-N6cHK9p715OUKmNiX5g.jpeg 640w, https://miro.medium.com/max/1400/1*gX-N6cHK9p715OUKmNiX5g.jpeg 700w" sizes="700px"/></noscript>

docker-nodejs

As a Node.js developer, chances are you have at least heard of Docker. If you are new to Docker, [this article](/free-code-camp/a-beginner-friendly-introduction-to-containers-vms-and-docker-79a9e3e119b) 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](/free-code-camp/a-beginners-guide-to-docker-how-to-create-your-first-docker-application-cc03de9b639f) and if you are new to Node.js, you can go through [this article](https://codeburst.io/getting-started-with-node-js-a-beginners-guide-b03e25bca71b) to get started.

Technologies To Be Used:

*   [**Express**](https://expressjs.com)
*   [**docker**](https://www.docker.com/)

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

*   [**Node.js**](https://nodejs.org)
*   [**Docker**](https://www.docker.com/)

**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](http://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](https://twitter.com/hybeecodes), [GitHub](https://github.com/hybeecodes) and [Instagram](https://www.instagram.com/hybeecodes/). Enjoy!!!.
