Dockerize your node.js application

In this article, we’re going to run a simple node application using docker. I’ll assume that you have some familiarity with docker and that you have docker installed and running. If not, visit to get set up.

Part 1: Setting up the node.js app

First, let’s build a simple node app and get it running locally without using docker. Make a new directory for this project and navigate to it in your terminal, then enter the following command:

This will create a file called and pre-populate it with some default configuration options (if you want to enter these details in manually, you can just run instead).

Next, we need to install the package, which we will use to set up a simple webserver in nodejs. Run the following to install it:

You should see that your file now has a “dependencies” section added to it with the express package listed along with the acceptable versions that can be used in this project.

Now that we’ve initialized our node app and installed our dependencies, create a file called and populate it with the following code:

Next, run in the command prompt and navigate to http://localhost:3000 in the browser. You should see that your app is up and running.

Part 2: Dockerizing the node.js application

Now that we have our hello world app up and running, let’s run it in a docker container. First, we’ll need a Dockerfile. This is essentially a blueprint of instructions for setting up the environment in which our node.js application will run. We’ll use this Dockerfile to build a docker image, which we can run as a docker container.

Create a new file called and populate it with the following code:

Let’s go through this file line by line:

  • Line 1 uses the official node.js docker image as the base image for our dockerfile. This essentially takes care of making sure that we have the latest stable version of node installed in our container.
  • Line 2 sets as the working directory inside our container. All commands run inside the container after this line will be run from this directory.
  • Line 3 copies the contents of our working directory on our host machine to the directory in the container
  • Line 4 runs the command from the working directory in our container (which we set to on line 2). This will use our file to install our specified dependencies.
  • Line 5 exposes port 3000 in our docker container, so that it can accept traffic to our app on that port.
  • Line 6 executes our javascript code in index.js to start up our express webserver. This command will execute immediately after our container is built and run.

Now that we have the Dockerfile set up, we can build our image and run our docker container. To build the image, run the following command:

This will build the image and tag it with the name “simple-node-app” using the instructions in the Dockerfile, which it will search for in the current directory (to access a Dockerfile in another directory, specify the path to that file instead of in the command above).

Once the image is built, we can run it as a container using the command:

However, if we try to visit our app at http://localhost:3000, we’ll find that it is inaccessible. This is because even though the app is listening at port 3000, the port this refers to is inside the container, not on our host machine. To fix this, we need to set up port-forwarding so that our local machine knows to forward traffic on port 3000 to the same port in docker. We can do this easily by modifying the as follows

Now if you visit the app in the browser, you should see that it is working successfully:

Congratulations! You just dockerized a node.js application. There’s much more to learn about Docker, but this is already a great start that you can use to apply as a starting point for your own applications. Please feel free to leave any questions in the comments below. Thanks for reading!

Professional Full Stack Web Developer and Backend/DevOps Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store