DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Docker for Dummies: Creating an Express+Node App

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.

Saurabh Sharma user avatar by
Saurabh Sharma
CORE ·
Oct. 03, 18 · Tutorial
Like (7)
Save
Tweet
Share
5.19K Views

Join the DZone community and get the full member experience.

Join For Free

Prologue

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.

Virtual Machine


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.

  1. Express generator to generate the application.
  2. Node as a container to host my application.
  3. 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.

Image titlePlease 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


Image title

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 .


Image title


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.

Image title

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.

Image title

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.


Docker (software) mobile app Express Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • How to Develop a Portrait Retouching Function
  • Public Cloud-to-Cloud Repatriation Trend
  • An Introduction to Data Mesh

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: