Application dependencies are difficult to keep track of.

Docker solves this problem by letting us package an application along with all its dependencies into a single image. The image can then be deployed as a container on a new machine with a fully configured environment.

In this tutorial, we will build a simple Hello World Node.js web app. We will then add a Dockerfile that lets us define instructions on how and what dependencies to install for the app to be functional. Using this file, we will build a new Docker image and deploy it as a container on our local machine.

Prerequisites

  1. Node.js
  2. Docker

Create a simple Node.js application

Create a directory where the project will live. Name it simple-nodejs-docker.

Create a new file called, package.json. This file will contain a description of the project and the dependencies needed.

{
   "name": "simple-nodejs-docker",
   "version": "1.0.0",
   "main": "app.js",
   "dependencies": {
     "express": "^4.16.3"
   },
   "scripts": {
     "start": "node app.js"
   }
 } 

Create a new file, app.js. Using Express, we will serve a simple / endpoint that will respond with Hello Docker’s World!.

const express = require("express");
const app = express();

app.get("/", (req, res) => res.send("Hello Docker's World!"));
	app.listen(3000, () => {
	console.log("Node.js app listening on port 3000!");
}); 

Run the web app

Install the dependencies by running,

npm install

Start the app.

npm start

The application should be live at http://localhost:3000/

Nice! Let’s keep moving.

Dockerizing the web app

Create a new file, Dockerfile

FROM node:9-slim
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
  • FROM node:9-slim: Pulls the base image from DockerHub that includes minimal packages to run Node.js.
  • WORKDIR /app: Sets the working directory for the project.
  • COPY package*.json /app: Copies both the package.json and package-lock.json to the app directory. Notice that we copy package.json and the rest of the application in separate steps. Docker can cache existing image layers, so if the application code changes but package.json remains the same, node_modules won’t be reinstalled.
  • RUN npm install: Installs the packages mentioned in package.json.
  • COPY . /app: Copies the rest of the application to the directory.
  • CMD [“npm”, “start”]: Starts the application.

Build Docker image

We can now start up Docker and use the DockerFile to build out an image by running the command,

docker build . -t sssaini/simple-docker
  • -t: Tag the image with a name.

Let’s now run the image as a container by running the command,

docker run -p 3001:3000 sssaini/simple-docker
  • -p: Binds port 3000 of the container to TCP port 3001 on the host machine.

If everything went well, our application should be live at http://localhost:3001.

bdcorps/nodejs-docker-boilerplate
A boilerplate Node.js web app with Docker support. Contribute to bdcorps/nodejs-docker-boilerplate development by creating an account on GitHub.

Next Steps

In this tutorial, we created a simple node.js web application and containerized it into a lightweight Docker image. We then deployed the image as a container on our local machine.

For the next steps, we can upload this image to the Docker Hub repository for later use.