Docker for Dummies: Creating an Express+Node App
Take a look at this tutorial that gives you some background information about Docker and shows you how to construct an application.
Join the DZone community and get the full member experience.
Join For FreePrologue
Between managing development teams, troubleshooting customer issues, and handling everyday backlog, the only thing that motivates me is trying out something new. Well, to be honest, not something entirely new, but definitely a mature technology that is a proven tool and has helped enable multiple organizations to solve their complex problems and for us, helped solve one of our major problem, The PSM (Product Support Matrix). Our problem was a multitude of platforms and equally verbose versions that we had churned out of our modern software factory.
What I knew before we ventured into the Docker world was the plain dictionary introduction:
It is a lightweight, open, secure platform to simplify building, shipping and running apps in isolated containers.
What intrigued me was not just the simplicity, but the evolution of virtualization. Specifically, how the technology evolved from Virtual Machine/Hypervisor to the leaner and better containers.
What Is a Container, and How Is It Different from an Image?
Image(s) are used to create containers, and a container is where the live application runs from. A container can be run, stopped, or deleted.
One needs to familiarize oneself with the tools and commands to understand the container universe.
Setup
It was not too time-consuming to learn and try out the different Docker commands, but first you need to install Docker CE (Community Edition) and check out a well-explained 5-minute read available at At the time of writing this blog, the version that I am using is 18.06.1-ce, build e68fc7a.
The first command everyone tries and that I did was:
docker --version
and I could see a string output spelling out the version - 18.06.
Objective 1: Run an Express + Node Application
What all do I need to run my express application.
- Express generator to generate the application.
- Node as a container to host my application.
- Serve the container from the application.
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications and it was perfect for me as I wanted smallest possible learning curve to host my application and serve it via containers.
Some details:
express --version
-> 4.16
Command I used to generate my application:
express MyFirst
It helped me generate my first application under folder MyFirst.
Now, after my application is done, I just need to try node start
and it serves a healthy welcome page.
Please note: I have modified my port from default 3000 to 8082.6
This application is being served from the host machine and not the container so the step 2 was to create a container and run to serve this application.
Like Maven or Gradle, there is a repository for the artifacts published for use by Docker — Docker Hub — and I planned to utilize this to build my application on top of the node (more information available on site).
Command issued"
docker search node
So my checklist had:
- Create Express site — done.
- Identify the image to use from Docker Hub — done (Node identified)
- Create and run the image — to be done.
Docker Build
Docker build is a command to build images from a context called Docker file. A point to consider is build is run by the Docker daemon and not CLI.
I created a file called "Dockerfile" (default "Dockerfile") in my directory and wrote in few lines (also explained below).
This file is a collection of commands that need to be executed and compiled into an image. Each statement is executed and the results are committed to a new image if required.
# My first docker file.
FROM node:latest
MAINTAINER Saurabh Sharma (XXXXXX@gmail.com)
RUN echo "Tryin to build my first application"
COPY . /var/www
WORKDIR /var/www
RUN npm install
EXPOSE 3000
ENTRYPOINT ["npm","start"]
Explanation
Line 2:
FROM node:latest
It defines the base image to start from. In our case, we need Node to host our application.
Line 6:
COPY . /var/www
Copies all the files from "." to "/var/www"
Line 7:
WORKDIR /var/www
Sets the working directory to any subsequent commands.
Line 9:
RUN npm install
executes commands in a new layer on top of the current image.
Line 11:
EXPOSE 3000
Which port the container will listen on. You can open more than one port using expose.
Line 13:
ENTRYPOINT
ENTRYPOINT ["executable", "param1", "param2"]
(
After saving this file, we need to build the image that when
docker run
will serve the express application.
docker build -t samarthya/node:v1.0.0 .
The command defines the -t
is a tag samarthya/node with version v1.0.0 and can have multiple tags as well, and the context directory denoted by a "."
Once you execute this command there will be many temporary/intermediary images generated as can be seen in the image.
The last message displays the status of the command executed. You can validate the image generated by issuing the Docker images command and it will display the image ID, tag and repository tag.
Now you can execute this image to spawn a container.
docker run -d -p 8082:3000 --name expressserver f09e3XSded
You can check if it started by invoking docker ps
and it will show the container processes running.
Time to check the express server app.
Browse to the site by entering the URL: http://hostname:8082/
Note: -p
is the mapping we have defined for the internal port.
Opinions expressed by DZone contributors are their own.
Comments