{"id":6898,"date":"2018-09-19T21:26:47","date_gmt":"2018-09-19T17:26:47","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/docker-guide-dockerizing-nodejs-express-application\/"},"modified":"2018-09-19T21:26:47","modified_gmt":"2018-09-19T17:26:47","slug":"docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04\/","title":{"rendered":"Docker Guide: Dockerizing Nodejs Express Application on Ubuntu 18.04"},"content":{"rendered":"<p>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.<\/p>\n<p>Nodejs is an open source JavaScript run-time environment. It&#8217;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.<\/p>\n<p>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.<\/p>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 18.04<\/li>\n<li>Root privileges<\/li>\n<\/ul>\n<h2 id=\"what-we-will-do\">What we will do<\/h2>\n<ol>\n<li>Install Docker CE<\/li>\n<li>Install Docker-Compose<\/li>\n<li>Setup Nodejs Express Project<\/li>\n<li>Build the Project<\/li>\n<li>Testing<\/li>\n<\/ol>\n<h2 id=\"step-install-docker-ce\">Step 1 &#8211; Install Docker CE<\/h2>\n<p>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.<\/p>\n<p>Before installing docker-ce, install docker dependencies needed using the apt command.<\/p>\n<p class=\"command\">sudo apt install -y \\<br \/>\u00a0\u00a0\u00a0 apt-transport-https \\<br \/>\u00a0\u00a0\u00a0 ca-certificates \\<br \/>\u00a0\u00a0\u00a0 curl \\<br \/>\u00a0\u00a0\u00a0 software-properties-common<\/p>\n<p>Now add the docker key and repository by running commands below.<\/p>\n<p class=\"command\">curl -fsSL https:\/\/download.docker.com\/linux\/ubuntu\/gpg | sudo apt-key add &#8211;<br \/>sudo add-apt-repository \\<br \/>\u00a0\u00a0 &#8220;deb [arch=amd64] https:\/\/download.docker.com\/linux\/ubuntu \\<br \/>\u00a0\u00a0 $(lsb_release -cs) \\<br \/>\u00a0\u00a0 stable&#8221;<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04.png\" alt=\"Install Docker\" width=\"500\" height=\"366\" title=\"\"><\/a><\/p>\n<p>Install the docker-ce package.<\/p>\n<p class=\"command\">sudo apt install docker-ce<\/p>\n<p>After the installation is complete, start the docker service and enable it to launch every time at system boot.<\/p>\n<p class=\"command\">systemctl start docker<br \/>systemctl enable docker<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-1.png\" alt=\"Start Docker\" width=\"500\" height=\"156\" title=\"\"><\/a><\/p>\n<p>Next, we will give the normal user privileges to run and manage docker container.<\/p>\n<p>Add a new user named &#8216;mohammad&#8217; and add it to the docker group.<\/p>\n<p class=\"command\">useradd -m -s \/bin\/bash mohammad<br \/>usermod -a -G docker mohammad<\/p>\n<p>Now login as the &#8216;mohammad&#8217; user and run docker container hello-world.<\/p>\n<p class=\"command\">su &#8211; mohammad<br \/>docker run hello-world<\/p>\n<p>And you will get the result as shown below.<\/p>\n<p><a class=\"fancybox\" id=\"img-3\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-2.png\" alt=\"Add user and test the user\" width=\"500\" height=\"450\" title=\"\"><\/a><\/p>\n<h2 id=\"step-install-dockercompose\">Step 2 &#8211; Install Docker-Compose<\/h2>\n<p>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.<\/p>\n<p>Download the latest 1.22.0 version of docker-compose using curl command to the &#8216;\/usr\/local\/bin&#8217; directory, and then make it executable using chmod.<\/p>\n<p>Run commands below.<\/p>\n<p class=\"command\">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<br \/>sudo chmod +x \/usr\/local\/bin\/docker-compose<\/p>\n<p>And the latest docker-compose version has been installed, check by running the docker-compose command below.<\/p>\n<p class=\"command\">docker-compose version<\/p>\n<p>Following is the result.<\/p>\n<p><a class=\"fancybox\" id=\"img-4\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-3.png\" alt=\"Install Docker Compose\" width=\"500\" height=\"151\" title=\"\"><\/a><\/p>\n<h2 id=\"step-setup-nodejs-express-project\">Step 3 &#8211; Setup Nodejs Express Project<\/h2>\n<p>In this section, we will configure the Nodejs application environment. We will be using simple nodejs application that can be generated using the &#8216;express-generator&#8217;.<\/p>\n<p>Login to the &#8216;mohammad&#8217; user.<\/p>\n<p class=\"command\">su &#8211; mohammad<\/p>\n<p>Create a new directory named &#8216;project&#8217; and go to it.<\/p>\n<p class=\"command\">mkdir -p project<br \/>cd project<\/p>\n<p>Now generate a simple hello-word nodejs application using the &#8216;express&#8217; command below.<\/p>\n<p class=\"command\">express hakase-app<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>Make sure nodejs, npm, and the &#8216;express-generator&#8217; packages have been installed on the system.<\/p>\n<p>And you will get the simple nodejs express app on the &#8216;hakase-app&#8217; directory.<\/p>\n<p><a class=\"fancybox\" id=\"img-5\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-4.png\" alt=\"Install Nodejs Express Project\" width=\"406\" height=\"550\" title=\"\"><\/a><\/p>\n<p>Next, we will build our custom docker image for the &#8216;hakase-app&#8217; nodejs application.<\/p>\n<p>Under the &#8216;project&#8217; directory, create a new &#8216;Dockerfile&#8217; using <a href=\"https:\/\/www.howtoforge.com\/vim-basics\" target=\"_blank\" rel=\"noopener noreferrer\">vim<\/a>.<\/p>\n<p class=\"command\">vim Dockerfile<\/p>\n<p>Paste the following docker image configuration there.<\/p>\n<pre>FROM node:8.11.4-alpine&#13;\n&#13;\nRUN mkdir -p \/src&#13;\nRUN npm install express-generator -g&#13;\n&#13;\nWORKDIR \/src&#13;\nADD hakase-app\/package.json \/src\/package.json&#13;\nRUN npm install&#13;\n&#13;\nEXPOSE 3000&#13;\nCMD node hakase-app\/bin\/www<\/pre>\n<p>Save and exit.<\/p>\n<p><a class=\"fancybox\" id=\"img-6\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-5.png\" alt=\"Use npm to install application\" width=\"500\" height=\"223\" title=\"\"><\/a><\/p>\n<p>We&#8217;re creating a new custom docker image for our nodejs application with specifications below.<\/p>\n<ul>\n<li>The custom image is based on the official nodejs image 8.11.4 alpine version.<\/li>\n<li>We&#8217;re creating a new directory for our project &#8216;\/src&#8217;.<\/li>\n<li>Install the &#8216;express-generator&#8217; to the custom image.<\/li>\n<li>Add the &#8216;package.json&#8217; file that contains application profile and packages needed to the &#8216;\/src&#8217; directory.<\/li>\n<li>Install all nodejs packages needed for the project, based on the &#8216;package.json&#8217; file.<\/li>\n<li>Export the port 3000 with default exec start command &#8216;node hakase-app\/bin\/www&#8217;.<\/li>\n<\/ul>\n<p>Next, we will build out the custom image based on the docker-compose script.<\/p>\n<p>Create a new yml file &#8216;docker-compose.yml&#8217; using vim.<\/p>\n<p class=\"command\">vim docker-compose.yml<\/p>\n<p>Paste the following configuration there.<\/p>\n<pre>version: '3'&#13;\n&#13;\nservices:&#13;\n&#13; hakase-app:&#13; build: .&#13; volumes:&#13; - .\/hakase-app:\/src\/hakase-app&#13; ports:&#13; - \"3000:3000\"&#13; restart: always<\/pre>\n<p>Save and exit.<\/p>\n<p><a class=\"fancybox\" id=\"img-13\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/13.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-6.png\" alt=\"Create docker compose yml file\" width=\"500\" height=\"231\" title=\"\"><\/a><\/p>\n<p>We&#8217;re creating a new docker service named &#8216;hakase-app&#8217;, and in the same time we&#8217;re building the custom docker image based on the &#8216;Dockerfile&#8217; on the project directory and will mount automatically the hakase-app application files.<\/p>\n<p>The Nodejs Express Setup has been completed.<\/p>\n<h2 id=\"step-build-and-run-the-project\">Step 4 &#8211; Build and Run the Project<\/h2>\n<p>Login as the &#8216;mohammad&#8217; user and go to the &#8216;project&#8217; directory<\/p>\n<p class=\"command\">su &#8211; mohammad<br \/>cd project\/<\/p>\n<p>Build the image and create the docker service hakase-app using the docker-compose command below.<\/p>\n<p class=\"command\">docker-compose build<\/p>\n<p>Now you will get the result as shown below.<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-7.png\" alt=\"Build docker project\" width=\"500\" height=\"388\" title=\"\"><\/a><\/p>\n<p>And when its complete, run the &#8216;hakase-app&#8217; docker service.<\/p>\n<p class=\"command\">docker-compose up -d<\/p>\n<p>Following is the result.<\/p>\n<p><a class=\"fancybox\" id=\"img-9\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-8.png\" alt=\"Use docker-compose up\" width=\"500\" height=\"237\" title=\"\"><\/a><\/p>\n<p>The custom docker image for our Nodejs application has been created, and the hakase-app docker service is up and running.<\/p>\n<h2 id=\"step-testing\">Step 5 &#8211; Testing<\/h2>\n<p>Show available docker image on the server.<\/p>\n<p class=\"command\">docker-compose images<br \/>docker images<\/p>\n<p><a class=\"fancybox\" id=\"img-10\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/10.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-9.png\" alt=\"Show available docker image\" width=\"500\" height=\"168\" title=\"\"><\/a><\/p>\n<p>A new custom docker image for our Nodejs application has been created.<\/p>\n<p>Show the running docker service.<\/p>\n<p class=\"command\">docker-compose ps<br \/>docker ps<\/p>\n<p><a class=\"fancybox\" id=\"img-11\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/11.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-10.png\" alt=\"Show the running docker service\" width=\"500\" height=\"108\" title=\"\"><\/a><\/p>\n<p>The Nodejs app is up and running on the host port 3000.<\/p>\n<p>Open the web browser and type the server IP address with the port.<\/p>\n<p><a href=\"http:\/\/10.0.15.2:3000\/\">http:\/\/10.0.15.2:3000\/<\/a><\/p>\n<p>And you will get the simple express page.<\/p>\n<p><a class=\"fancybox\" id=\"img-12\" href=\"https:\/\/www.howtoforge.com\/images\/docker_guide_dockerizing_nodejs_express_application\/big\/12.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-11.png\" alt=\"Page is working\" width=\"500\" height=\"120\" title=\"\"><\/a><\/p>\n<p>Dockerizing Nodejs Express application on Ubuntu 18.04 has been completed successfully.<\/p>\n<h2 id=\"reference\">Reference<\/h2>\n<div class=\"authorbox\" readability=\"15\">\n<p><strong>About Muhammad Arul<\/strong><\/p>\n<p>Muhammad Arul is a freelance system administrator and technical writer. He is working with Linux Environments for more than 5 years, an Open Source enthusiast and highly motivated on Linux installation and troubleshooting. Mostly working with RedHat\/CentOS Linux and Ubuntu\/Debian, Nginx and Apache web server, Proxmox, Zimbra Administration, and Website Optimization. Currently learning about OpenStack and Container Technology.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fdocker-guide-dockerizing-nodejs-express-application%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-12.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fdocker-guide-dockerizing-nodejs-express-application%2F&amp;text=Docker+Guide%3A+Dockerizing+Nodejs+Express+Application+on+Ubuntu+18.04&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-13.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-14.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fdocker-guide-dockerizing-nodejs-express-application%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/docker-guide-dockerizing-nodejs-express-application-on-ubuntu-18-04-15.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s multi-platform run-time, can be installed on Linux, Windows, MacOS, FreeBSD, etc. Nodejs &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-6898","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=6898"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6898\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=6898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=6898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=6898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}