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 https://www.docker.com/ 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:
npm init -y
This will create a file called
package.json and pre-populate it with some default configuration options (if you want to enter these details in manually, you can just run
npm init instead).
Next, we need to install the
express package, which we will use to set up a simple webserver in nodejs. Run the following to install it:
npm install --save express
You should see that your
package.json 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
index.js and populate it with the following code:
node . 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
Dockerfile 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
/appas 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
/appdirectory in the container
- Line 4 runs the
npm installcommand from the working directory in our container (which we set to
/appon line 2). This will use our
package.jsonfile 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.
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:
docker build -t simple-node-app .
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
docker run command:
docker run simple-node-app
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
docker-run command as follows
docker run -p 3000:3000 simple-node-app
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!