Docker: dockerize webapplication (with Dockerfile)

By | 10/03/2016


In this post, we will create a simple Express web application and build our own image from that. In a next post, we will also upload the image to the docker hub repository so other people could use it as well.

Create Express webapplication

First, let’s create a simple Express web application. Note that we store every file in the folder /home/cloud-user/express-app.

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World wapptastic')

app.get('/info', function (req, res) {
  res.send('This is the info page')

var server = app.listen(3001, function () {

  var host = server.address().address
  var port = server.address().port

  console.log('Example app listening at http://%s:%s', host, port)


I believe the code is rather straightforward. Whenever someone is visiting the root of our webapplication, we will return back the string ‘Hello World wapptastic’ and whenever someone goes to the /info route, we will return back the string ‘This is the info page’.

As we have seen in previous posts, every Express app needs a package.json file to provide metadata and toresolve the dependencies. Here is the package.json file for our example:

root@ubuntu-demo:/home/cloud-user/express_app# cat package.json
  "name": "docker-ubuntu-express",
  "private": true,
  "version": "0.0.1",
  "description": "Express application displaying Hello world on Ubuntu using docker",
  "author": "Wim Wauters ",
  "dependencies": {
    "express": "3.2.4"

The Docker part now…

Next up is the docker configuration. As mentioned before, the better way of creating your own images is by creating a Dockerfile. This file basically contains a set of instructions that tell Docker how to build our image.

Create the Dockerfile

FROM ubuntu:14.04

# Enable EPEL for Node.js
RUN apt-get update
RUN apt-get -y install build-essential
RUN apt-get -y install nodejs
RUN apt-get -y install npm
RUN apt-get -y install git
RUN apt-get -y install git-core

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE  3001
CMD ["nodejs", "/usr/src/app/index.js"]

Build the image

root@ubuntu-demo:/home/cloud-user/express_app# docker build -t wiwa1978/ubuntu-express-app .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM ubuntu:14.04
 ---> c9ea60d0b905
Step 2 : RUN apt-get update
 ---> Using cache
 ---> 4f0fe115ac4f
Step 3 : RUN apt-get -y install build-essential
 ---> Using cache
 ---> 0a85186b8534
Step 4 : RUN apt-get -y install nodejs
 ---> Using cache
 ---> 609701a297c0
Step 5 : RUN apt-get -y install npm
 ---> Using cache
 ---> 3d825e628dcd
Step 6 : RUN apt-get -y install git
 ---> Using cache
 ---> f4daada71496
Step 7 : RUN apt-get -y install git-core
 ---> Using cache
 ---> 82743c3f2046
Step 8 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> b1229835f0b0
Step 9 : WORKDIR /usr/src/app
 ---> Using cache
 ---> e44fb8877894
Step 10 : COPY package.json /usr/src/app/
 ---> Using cache
 ---> e89f5d8a7537
Step 11 : RUN npm install
 ---> Using cache
 ---> 985ecf42d9fd
Step 12 : COPY . /usr/src/app
 ---> Using cache
 ---> 9b040de63457
Step 13 : EXPOSE 3001
 ---> Using cache
 ---> f07e565d49b6
Step 14 : CMD nodejs /usr/src/app/index.js
 ---> Using cache
 ---> 22d235d53e91
Successfully built 22d235d53e91

Running the ‘docker images’ command will show indeed that an image was created with name wiwa1978/ubuntu-express-app

root@ubuntu-demo:/home/cloud-user/express_app# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
wiwa1978/ubuntu-express-app   latest              22d235d53e91        5 minutes ago       408.6 MB
wiwa1978/fedora-httpd         latest              5fa2fee12007        57 minutes ago      573.3 MB
ubuntu                        14.04               c9ea60d0b905        6 days ago          188 MB
centos                        latest              28e524afdd05        11 days ago         196.7 MB
fedora                        latest              9bdb5101e5fc        5 weeks ago         204.7 MB
training/webapp               latest              54bb4e8718e8        11 months ago       348.8 MB

Running the image we just created

root@ubuntu-demo:/home/cloud-user/express_app# docker run -it -p 3001:3001 wiwa1978/ubuntu-express-app
Example app listening at

In the output, you can see that we have our app listing to port 3001. Note that we have mapped the port 3001 of our host system to the port 3001 within our container as in the index.js file we have set the TCP port for our webapp indeed to 3001. See below the screenshot as a proofpoint.


Leave a Reply

Your email address will not be published. Required fields are marked *