Skip to content
A quickstart example how to get a Node.js application into a Docker container.
Dockerfile JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Quickstart to dockerizing a Node API

The goal of this project is training you in how to get a Node.js application running into a Docker container.

This guide assumes that you have a working Docker installation and a basic understanding of how a Node.js application is structured.

Creating the Node.js app

We need to create a directory to save all files of our project. So:

$ mkdir study-dockerizing-a-node-api
$ cd study-dockerizing-a-node-api

After create the directory, we need a package.json to create a Node.js API:

$ npm init

After that install the express dependency:

$ npm i express --save

Edit and create a new script to start the node server in the package.json:

"start": "node index.js",

Now we have a package.json:

  "name": "study-dockerizing-a-node-api",
  "version": "0.0.1",
  "description": "A repository containing an article and example to start my docker studies",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  "repository": {
    "type": "git",
    "url": "git+"
  "keywords": [
  "author": "Mateus Malaquias",
  "license": "MIT",
  "bugs": {
    "url": ""
  "homepage": "",
  "dependencies": {
    "express": "^4.16.4"

Creating a sample API

Now create a index.js file that defines a web app using the Express.js framework:

const express = require('express');

const PORT = 8080;
const HOST = '';

const app = express();

app.get('/', (req, res) => {
  res.send('Hello world\n');

app.listen(PORT, HOST);

console.log(`Running on http://${HOST}:${PORT}`);

Creating a Dockerfile

A Dockerfile is a text document that contains all the commands that a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Create an empty file called Dockerfile:

$ touch Dockerfile

Open this file with your favorite text editor. And lets go!

$ vim Dockerfile

Your Dockerfile should look like this:

# Node runtime version
FROM node:8

# The api directory inside of the docker image

# A wildcard is used to ensure both package.json AND package-lock.json are copied to the docker image
COPY package*.json ./

# Copy the project to the docker image
COPY . .

# Install the API dependencies with NPM
RUN npm install

# Informe to the docker, the door let's use

CMD ["npm", "start"]

TL; DR Docker atrributes

Building your image

Go to the directory that has your Dockerfile and execute the following command to build the Docker image.

$ docker build -t <your username>/node-hello-world .

Run the image

Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container.

$ docker run -p 8080:8080 -d <your username>/node-web-app


Open your web browser or postman application and make a request to the URL above:


Or use the curl application inside the terminal.

$ curl -i localhost:49160

You need to see a simple Hello world message.

I hope this tutorial helped you get up and running a simple Node.js application on Docker.

BONUS: Creating Docker ignore file

Before creating a docker image it's intresting to make a file named .dockerignore in the root directory. When this file exists the CLI modifies the context to exclude files and directories that match patterns in it. This will help us avoid unnecessarily sending large or sensitive files and directories to the image when using ADD or COPY command.

So create the file and edit it:

$ touch .dockerignore
$ vim .dockerignore

Write the content above:


This will prevent your local modules and debug logs from being copied into your Docker image.



Please read for details on our code of conduct, and the process for submitting pull requests to us.


See also the list of contributors who participated in this project.


This project is licensed under the MIT License - see the file for details

You can’t perform that action at this time.