Docker Explained. Easy steps to get started with docker.

Docker Introduction

Docker is used for isolating and standardizing the development environment. For any script to run it needs to have its own environment and dependencies installed. Looking at higher level product development, there is a large number of dependencies and system libraries required for an application to run. All these libraries and dependencies are not fixed, you keep adding new functionalities, and give out new versions. It is very hard to move or recreate the same application in different environments.

Docker solves this problem by creating containers independent to host os. You can even run your own os on Docker containers. Let’s see how these docker containers are created and can be used to move and recreate the same runtime across different platforms/environments.

Dockerfile Example –

A Dockerfile contains instructions to build docker images.

Example –

  1. FROM node:9-stretch
  2. COPY . /app/
  3. RUN npm install
  4. EXPOSE 8080
  5. CMD node server.js

“FROM node:9-stretch” creates base os to the image (node:9-stretch is an official docker image with nodejs dependencies pre-installed on Debian stretch).

“COPY . /app” loads your source files into the app folder inside a container.

“RUN npm install” downloads npm dependencies

“CMD node server.js” starts nodejs application and

“EXPOSE 8080” exposes port 8080 to the host machine.

Once you got your Dockerfile setup, create a Dockerfile ( nano Dockerfile ), place your docker file inside your source folder and use docker build command to create Docker Image from it.

Docker Image Command –

# Build an image using the Dockerfile at the current location
# Example: docker build -t [name] .

# there is a dot at the end of the command

docker build -t simple_nodejs_image .


Here is the complete example of a Dockerfile –

FROM node:9-stretch

# Create app directory


# Install app dependencies

RUN apt-get update

RUN apt-get -y install ffmpeg

# Bundle app source

COPY . /app/

RUN npm install

RUN npm install pm2 -g

# If you are building your code for production

# RUN npm install --only=production


CMD NODE_ENV=development pm2-docker server.js

Sample Folder Structure –


Docker Images –

Docker image is an artifact, essentially a snapshot of the read-only contents a container is meant to run. Each command/ instruction in the Dockerfile creates the new layer to the image. FROM directive defines the base image ( base layer) to start the build process. And each RUN command is used to build the image forming another layer on top of the previous one.

Unlike RUN, CMD is not executed during build. CMD would be running an application upon creation of a container which is already installed using RUN.


Docker Containers –

Using the image we can now create a container running a nodejs application inside and exposing 8080 port to host system.

Command Example –

docker run -name simple_nodejs_container -i -t simple_nodejs_image


Docker Compose –

Docker compose is a tool for defining and running multiple docker containers together. YAML file is used to define your application services. Rather creating each docker container and configuring the communication between them manually, You can use Compose command line tool to create and start one or more containers for each dependency with a single command.(docker-compose up).

Example for docker-compose.yml

version: "2"



   build: .


     - "8080:8080"


     - "mongo"


     - . :/app


   image: mongo


     - "27017:27017"

Here, we have two services “web” and “mongo”. First service “web” creates our app environment defined in our docker file and Second service “mongo” creates independent containers from official docker mongo image. “depends_on” command gives information to “web” service container to communicate with “mongo” service container on port 27017.

Docker Compose is basically a three-step process :

  • Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  • Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  • Run docker-compose up and Compose starts and runs your entire app.

Conclusion –

VMs can also be used to solve this problem but docker uses a fraction of the computing power leveraging low-level mechanics of the host operating system compared to VMs which has high computational overhead.

Leave a Reply

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

eighteen − two =