Dockerize Nodejs Express Application on Ubuntu 18.04 Print this Article
Step 1 - Install Docker CE
In this tutorial, we will install docker-ce community edition from the docker repository. We will install docker-ce community edition and docker-compose that supports compose file version 3. Before installing docker-ce, install docker dependencies needed using the apt command.
sudo apt install -y \ apt-transport-https \ ca-certificates \ curl \ software-properties-common
Now add the docker key and repository by running commands below.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
Install the docker-ce package.
sudo apt install docker-ce
After the installation is complete, start the docker service and enable it to launch every time at system boot.
systemctl start docker systemctl enable docker
Next, we will give the normal user privileges to run and manage docker container. Add a new user named 'josh-servercheap' and add it to the docker group.
useradd -m -s /bin/bash josh-servercheap usermod -a -G docker josh-servercheap
Now login as the 'josh-servercheap' user and run docker container hello-world.
su - josh-servercheap docker run hello-world
Step 2 - Install Docker-Compose
In this step, we will install docker-compose manually from the binary file that can be downloaded from GitHub. We will install the latest docker-compose version that will support the compose v3. Download the latest 1.22.0 version of docker-compose using curl command to the '/usr/local/bin' directory, and then make it executable using chmod. Run commands below.
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
And the latest docker-compose version has been installed, check by running the docker-compose command below.
Step 3 - Setup Nodejs Express Project
In this section, we will configure the Nodejs application environment. We will be using simple nodejs application that can be generated using the 'express-generator'. Login to the 'josh-servercheap' user.
su - josh-servercheap
Create a new directory named 'project' and go to it.
mkdir -p project cd project
Now generate a simple hello-word nodejs application using the 'express' command below.
Note: Make sure nodejs, npm, and the 'express-generator' packages have been installed on the system. And you will get the simple nodejs express app on the 'hakase-app' directory.
Next, we will build our custom docker image for the 'hakase-app' nodejs application. Under the 'project' directory, create a new 'Dockerfile' using vim.
Paste the following docker image configuration there.
FROM node:8.11.4-alpine RUN mkdir -p /src RUN npm install express-generator -g WORKDIR /src ADD hakase-app/package.json /src/package.json RUN npm install EXPOSE 3000 CMD node hakase-app/bin/www
Save and exit.
We're creating a new custom docker image for our nodejs application with specifications below.
The custom image is based on the official nodejs image 8.11.4 alpine version.
We're creating a new directory for our project '/src'.
Install the 'express-generator' to the custom image.
Add the 'package.json' file that contains application profile and packages needed to the '/src' directory.
Install all nodejs packages needed for the project, based on the 'package.json' file.
Export the port 3000 with default exec start command 'node hakase-app/bin/www'.
Next, we will build out the custom image based on the docker-compose script.
Create a new yml file 'docker-compose.yml' using vim.
Paste the following configuration there.
version: '3' services: hakase-app: build: . volumes: - ./hakase-app:/src/hakase-app ports: - "3000:3000" restart: always
Save and exit.
We're creating a new docker service named 'hakase-app', and in the same time we're building the custom docker image based on the 'Dockerfile' on the project directory and will mount automatically the hakase-app application files. The Nodejs Express Setup has been completed.
Step 4 - Build and Run the Project
Login as the 'josh-servercheap' user and go to the 'project' directory
su - josh-servercheap cd project/
Build the image and create the docker service hakase-app using the docker-compose command below.
And when its complete, run the 'hakase-app' docker service.
docker-compose up -d
The custom docker image for our Nodejs application has been created, and the hakase-app docker service is up and running.x
Step 5 - Testing
Show available docker image on the server.
docker-compose images docker images
A new custom docker image for our Nodejs application has been created. Show the running docker service.
docker-compose ps docker ps
The Nodejs app is up and running on the host port 3000.
Open the web browser and type the server IP address with the port.
And you will get the simple express page.
Dockerizing Nodejs Express application on Ubuntu 18.04 has been completed successfully.
Dockerize Nodejs on one of our Ubuntu servers today.Your best VPS hosting option is here: $4.50/month
Was this answer helpful?
Package rebuilding is something easily done in Debian. As SysAdmin, you might find yourself in...
I wanted to use PHP 7 on Debian 8.x. How do I install and configure PHP 7 on Debian Linux 8.x...
EasyEngine (ee) is a Linux shell-script to install and manage wordpress-nginx websites in one...
LAMP is short for Linux, Apache, MySQL, PHP. This tutorial shows how you can install an Apache 2...
Introduction As your application or website grows, you may come to a point where you’ve...