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 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:

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.

  • Line 2 sets /app 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 /app directory in the container
  • Line 4 runs the npm install command from the working directory in our container (which we set to /app on line 2). This will use our package.json 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.

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