Docker Guide: Dockerizing Nodejs Express Application on Ubuntu 18.04
Docker is an open-source project that provides an open platform for developers and sysadmins to build, package, and run applications anywhere as a lightweight container. Docker automates the deployment of applications inside software containers.
Nodejs is an open source JavaScript run-time environment. It’s multi-platform run-time, can be installed on Linux, Windows, MacOS, FreeBSD, etc. Nodejs is very useful for building both server and desktop applications.
In this tutorial, I will show you how to create a docker image for an existing Nodejs Express application project in Ubuntu 18.04. We will learn about dockerizing a Nodejs application, and then deploy the application as a container to the docker environment using a docker-compose script.
Prerequisites
- Ubuntu 18.04
- Root privileges
What we will do
- Install Docker CE
- Install Docker-Compose
- Setup Nodejs Express Project
- Build the Project
- Testing
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 ‘mohammad’ and add it to the docker group.
useradd -m -s /bin/bash mohammad
usermod -a -G docker mohammad
Now login as the ‘mohammad’ user and run docker container hello-world.
su – mohammad
docker run hello-world
And you will get the result as shown below.
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.
docker-compose version
Following is the result.
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 ‘mohammad’ user.
su – mohammad
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.
express hakase-app
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.
vim Dockerfile
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.
vim docker-compose.yml
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 ‘mohammad’ user and go to the ‘project’ directory
su – mohammad
cd project/
Build the image and create the docker service hakase-app using the docker-compose command below.
docker-compose build
Now you will get the result as shown below.
And when its complete, run the ‘hakase-app’ docker service.
docker-compose up -d
Following is the result.
The custom docker image for our Nodejs application has been created, and the hakase-app docker service is up and running.
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.