Integrating Docker With Node Express

Lucas Thinnes
4 min readJul 2, 2021

In the realm of doing what I can to demystify as many programming languages and concepts as I possibly can, I came across Docker. On their site, it claims to “simplify and accelerate development workflows with an integrated dev pipeline and through the consolidation of application components”. More specifically, it lets users “containerize” applications and run each constituent operation of the application in its own environment, or container. What exactly is a container? It is an isolated environment, meaning the processes running in one container will not interfere with another and will ultimately share the same OS kernel. This is all very high abstraction stuff, but should hopefully paint a clear picture as to why Docker is so powerful and highly utilized in this day and age. In this article, I want to start with a fundamental exercise of linking an Express backend to a Docker port!

INSTALLATION

First, you will want to make sure that you have Docker installed. If plugging “which docker” into your terminal results in some form of error, head over to the Docker Desktop page and download the correct version for your OS. Once you have it so entering “which docker” into your terminal gives you a location of a directory, you will be ready to move forward.

EXPRESS SETUP

To create your Express backend, create & navigate to a new & empty directory. Express apps are initialized by typing “npm init -y” while inside of the blank directory to create a package.json, and then running “npm i express”. Running “touch index.js” will create an empty JavaScript file for use to insert our Express data, which will look as such:

const app = require('express')()app.get('/', (request, response) => {   response.json({ message: 'This is a node app.'})})app.listen(9000)

To break this down in case you are new to express:

  • Express is brought in via the “require” method.
  • The app.get initiates the request / response cycle with an endpoint as a string.
  • The response is assigned to a JSON object with a message key / value pair.
  • The port is assigned to 9000 as it is generally unused.

In your terminal, run “node index.js” and navigate to your browser. What you should see upon navigating to http://localhost:9000/ should look like:

{
"message": "This is a node app."
}

DOCKER SETUP

Next up we want to type “touch Dockerfile” into our terminal (capitalization is important). The Dockerfile will manage the configuration of the output and its containers. We will start by giving it a version of Node to run, which the list of all versions can be found here. I am going to use the most recent as of writing this, “16-alpine3.11”. We want to define this in our Dockerfile by typing “FROM node:16-alpine3.11”. Next, we want to designate the working directory on the auxiliary machine that will be run via Docker, this will be done with the line of code “WORKDIR /app”. The port will need to be listed next which is done via an EXPOSE method, in this case “EXPOSE 9000”. Now, the Express backend needs to be added and launched from the Dockerfile, which will be done in two lines of code: “ADD . /app” and “CMD node index.js”. To clarify, our Dockerfill will look as such:

FROM node:16-alpine3.11WORKDIR /appEXPOSE 9000ADD . /appCMD node index.js

CREATING AND RUNNING THE CONTAINER

Next up we have to create the container. The command to do this is as follows:

sudo docker build -t express-backend .

To break this down, “sudo” gives root access, “docker build” actually builds the container, and “-t” stands for “tag” or name, and the name was “express-backend”. Finally, Docker needs to know where to look for the Express backend we built, and as it is all in the same directory, this command is closed with a period. This should configure over the course of about 10–15 seconds. Finally, to run the container, we use this command:

sudo docker run -p 9000:9000 -t express-backend

If all is successful, when you refresh your localhost:9000 page, it should run exactly the same except for within a Docker container!

I look forward to further illustrating the potential of Docker in the future.

--

--