Node.js Learning Notes 9 - Deploying Node applications to Docker

Deploying Node applications to Docker

It takes about 10 minutes to read this blog

Docker is an open source application container engine based on the Go language and compliant with the Apache 2.0 protocol.

Docker s can allow developers to package their applications and dependencies into a lightweight, portable container that can then be published to any popular Linux machine, or they can be virtualized.

Docker's advantages:

  • Deliver applications quickly and consistently.
  • Responsive deployment and expansion.
  • Run more workloads on the same hardware.

Good code is short, simple, and symmetrical—the challenge is figuring out how to get there. --Sean Parent


Node.js itself is a fast-building application environment that works well with Docker.The main content includes two parts:

  1. Create a simple REST application with Express
  2. Deploying services to Docker

Create a simple REST application with Express

  1. Get ready
mkdir a09

npm init -y

npm install express --save

Create a data.json:

{"name": "Hello", "likes": ["wolrd", "node"]}
  1. Server Code


const data = require('./data');
const express = require('express');

const PORT = 3000;
const HOST = '';

const app = express();

app.get('/user', (req, res) => {

app.listen(PORT, HOST);

console.log(`Running on http://${HOST}:${PORT}`);
  1. Test:
    Run the program and access it through the browser:

The returned JSON data can be viewed.

Deploying services to Docker

  1. Build Dockerfile

Create a new Dockerfile:

FROM node:latest

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

CMD [ "node", "server.js" ]
  1. Create a new.dockerignore file:
  1. Build Docker Mirror

Build a mirror with docker build s:

docker build -t <your username>/<your imagename> .

On the command line, type:

docker build -t rang/a09 .


Sending build context to Docker daemon  20.99kB
Step 1/7 : FROM node:latest
 ---> 2f1ff44a8bb5
Step 2/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> e57bfbf7a84e
Step 3/7 : COPY package*.json ./
 ---> a820cfd03931
Step 4/7 : RUN npm install
 ---> Running in 50a219d625ad
npm WARN exp01@1.0.0 No description
npm WARN exp01@1.0.0 No repository field.

added 50 packages from 37 contributors and audited 126 packages in 97.618s
found 0 vulnerabilities

Removing intermediate container 50a219d625ad
 ---> 419121cda361
Step 5/7 : COPY . .
 ---> 16db3371aff5
Step 6/7 : EXPOSE 3000
 ---> Running in 05b89cf2b4ff
Removing intermediate container 05b89cf2b4ff
 ---> 96359e1075c8
Step 7/7 : CMD [ "node", "server.js" ]
 ---> Running in 95cb9f195d76
Removing intermediate container 95cb9f195d76
 ---> bcf82dfe8b41
Successfully built bcf82dfe8b41
Successfully tagged rang/a09:latest

It's built in about a few seconds.

  1. Run Container:
docker run -p 3000:3000 -d rang/a09

Containers also start in seconds.

  1. Test:

Services can be accessed through a browser:



You can see that a REST API was quickly developed and then deployed locally through Docker.With Node.js and Docker, you can deploy cloud apps very quickly.

Project Code

Reference reading

266 original articles were published, 58 were praised, 1.2 million visits+
His message board follow

Tags: Docker npm JSON REST

Posted on Fri, 07 Feb 2020 18:10:57 -0800 by y2yang