April 2th

Docker nodejs express tutorial. Dockerize nodejs app in 3 minutes.

In this brief tutorial we present how you can run nodejs in docker with our service.

Step 1 - Create a simple nodejs app

First, you should initialize a new package

npm init -y

and install express framework.

npm install express --save

Good. Now you have package.json file with express as a dependency in it. Here is hello world nodejs app in a single file server.js

// server.js

const express = require('express');

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

//Listen port
const PORT = 8080;
console.log(`Running on port ${PORT}`);

Good. We have a nodejs app. To test it you can run:

node server.js

The app should be availbale on http://localhost:8080

header image

Step 2 - Make a docker container

Now let's copy a Dockerfile from official Nodejs website.

// Dockerfile:

FROM node:10

# 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
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" ]

We don't want a node_modules folder in our docker container. All dependencies should be installed from scratch. Let's create a .dockerignore file and prevent node_modules from copying to the container.

// .dockerignore:

Done with files. This is your files structure:

header image

Now let's build the container:

docker build -t imagename .

You can try to run it:

docker run -it --rm -p 8080:8080 imagename

Result should be the same:

header image

Step 3 - Push to cloud

In order to install node js docker container, create a new app via cli or admin panel and set a port to 8080.

header image

Now just push the container:

docker login -u (login to show username) -p (login to show password)

docker push to show username)/imagename

Done. The link to app is in control panel.

header image

Bonus 1: SSL certificate for HTTPS

It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.

Bonus 2: Autoscaling

With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.

Logs and app status

header image

Now you can deploy nodejs with docker without a massive build time.

Dockerize your Job

Just for $2/month
Share this article:

Ask a question

Please type your name.
Please provide your email address.
Please tell us a bit more

More great guides

Docker nodejs express tutorial

Docker NodeJS Express guide

Simple Nodejs Socket guide for docker deploy. Use it for comfort start with us.

Read Guide
Docker python django tutorial

Docker Python Django tutorial

This is a brief tutorial that presents how you can run a simple 'Hello World'-style web application written in Django in docker.

Read Guide
Docker PHP laravel tutorial

Docker PHP laravel tutorial

This quickstart guide provides a basic introduction to the Laravel framework running on docker. This is a great starting point if you are brand new to the Laravel framework or if you want to have your docker PHP app.

Read Guide
7 times faster deployment
Dockerize automaticaly completes deployment after receiving your Docker image
50% of AWS price
We don’t have any hidden prices and will never ask you to pay extra fee for our services