Docker: A Beginner’s Overview

Sejal Goyal
6 min readJun 21, 2020
Source: Google Images

Docker: an open-source tool(written in Go lang.) used to automate the deployment of applications in lightweight containers so they perform efficiently in different environments.

Docker helps in reducing requirement matching overhead between different environments.

For example, an app developed in Linux, which might also use MySQL DB is given for testing. Now the tester might need to have the same environment for testing, in which the app was initially made, which adds a lot of overhead to the full testing process.
Docker helps avoid this. Using docker, we can significantly reduce the delay between writing code and running it in production.

Time Consumption Average for deploying an application:
With Docker: 7–8 Minutes
Without Docker: 1.5–2 Hours

Using Docker, the developer bundles up all the required settings and applications in an image and the tester can create a container from the image.

Containers are very lightweight & so don’t take much time to run the application on the tester’s machine.

As you can see Docker is really beneficial in deploying a product.
In simple words, on a system, you can set up different environments(OS, database, servers etc.) for the product, but wait a second, we can do this with virtualization also(?). Why Docker, then?

Virtualization vs Containerization

Let’s have a look at the difference between virtualization and containerization:-

Source: Google Images

Virtualization: For creating virtual machines(VMs), the system uses hypervisor: a firmware layer that allows the system to run multiple OS’s on a single system. The hypervisor separates available resources (computing power, memory, storage, etc.), assigning a portion to each VM as needed.

Containerization: OS-level virtualization which allows multiple isolated user-space instances on a single kernel. These instances are called containers i.e. multiple containers can run on a single OS.
These containers are very lightweight and easy to install on the system. The most common container technology is Docker.

In short, your use case matters.
Containers are a good choice for projects with a much shorter life-cycle. Setting up a container in a system takes very less time so they are suitable for tasks that may only take a few hours.
Virtual machines have a longer life-cycle than containers and are best used for longer periods of time.

How Containerization Works

Docker uses namespaces to isolate workspaces. Process IDs, Network, Unix Time-sharing systems, Mount, Inter-Process Communication are created in their own namespaces, thereby providing isolation between containers.

The Docker Architecture:-

The Docker architecture uses a client-server model and comprises of the Docker Client, Docker Host, Network and Storage components, and the Docker Registry/Hub.

Source: Google Images

The Docker Engine allows user to develop and run an application using the following Docker components.

  1. Docker Daemon: A persistent background process that manages Docker images, containers, networks, and storage volumes. The Docker daemon constantly listens for Docker API requests and processes them.
  2. Docker Engine REST API: An API is used by applications to interact with the Docker daemon. It can be accessed by an HTTP client.
  3. Docker CLI: A command-line interface client for interacting with the Docker daemon. It significantly simplifies how you manage container instances and is one of the key reasons why developers love using Docker.

Docker Client talks to the Docker Daemon, which performs the heavy lifting of the building, running our Docker Containers.
Fundamentally, both the Docker Client and Daemon can run on the same system however, we could also connect a Docker Client to a remote Docker Daemon.

“docker run ubuntu” — How does it work?

The computer makes a request to the configured Docker Host API, which in turn interacts with the Docker Daemon.
The daemon, then looks up the Ubuntu image on the host registry. If it’s not present, a new lookup will be made for pulling (downloading) the image from the configured registry (Docker Hub).
Docker then creates(& runs) a container from the pulled image.

A Little Practical

Setting up the Docker environment:
Install Docker for Mac
Install Docker for Windows
Get Docker CE for Ubuntu

  1. Basic commands:

Shows information about client and server:

$ docker info

info also shows running containers, images available in the system etc.

Docker version — detailed information about client & server’s docker’s environments.

$ docker version

Docker version:

$ docker -v

2. Images: A Docker Image is an immutable file, comprised of multiple layers and it contains the source code, libraries, dependencies, tools, and other files needed for an application to run.

Lists all the images:

$ docker images

Shows the history of an image:

$ docker history <image name>

Shows information about the image.

$ docker inspect image

Pulls an image from the registry:

$ docker pull <image name>

Removes the image:

$ docker rmi <image name>

3. Containers (Running instances of docker images) :

Lists all the running containers:

$ docker ps

Lists all the containers(running & stopped):

$ docker ps -a

Creates a container:

$ docker run <image name>

The above command creates a container of the specified image.
If the image doesn’t exist in the system then this command pulls this image from Docker hub.

Create a container in iterative mode & a bash CLI in it.

$ docker run -it <image name> sh

Start and stop the container:

$ docker stop <container id>
$ docker start <container id>

Remove containers:

$ docker container rm <container id1> < container id2>……

4. Volume:

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. Deleting a container also deletes the data inside it.

Create a volume:

$ docker volume create my-vol

Inspect a volume:

$ docker volume inspect my-vol

Starting a container in detached mode with volume:

$ docker run -d -v myvol2:/app nginx:latest

All the data in the “app” directory gets stored in myvol2.
The path of myvol2 can be fetched using the inspect command.

Removing the volume:

$ docker volume rm my-vol

5. dockerfile

It is a file with instruction for building a Docker image. It uses a layered architecture. When Docker build the images it builds these in a layered architecture. Each line of instruction creates a new layer in the docker image with just the changes from the previous one.

Source: Google images

You can see these layers using “docker history” command. Using docker build, you can see the various steps involved and the result of each task. All the layers built are cached so the layered architecture helps you restart docker build from that particular layer.
For example, if a build fails at step-3, on building the file again it will use cached layer2 and the build will start from there itself.

Creating a dockerfile.

Step-1:- Create a folder and inside that folder create a file named Dockerfile.

Step-2:- Edit Dockerfile:-

Instructions of Dockerfile:- (Instructions are not case sensitive)

Few instructions of dockerfile.

1. FROM :- Download the image from Docker Hub.

Ex:- FROM ubuntu or FROM ubuntu:<tag>

Create an image from scratch

Ex:- FROM scratch

2. MAINTAINER :- Specify the name of developer.

Ex:- MAINTAINER Sejal Goyal <sejalgoyal1998@gmail.com>

3. RUN :- Instruction used to pass commands onto the image.

Ex. RUN apt-get update

4. CMD :- Defines the default executable of a docker image. This gets executed after creation of the image.

Ex. CMD [“ECHO”, “Hello World”]

5. WORKDIR:- This instruction sets the working directory for any instruction (RUN, CMD, ENTRYPOINT, COPY, ADD) that follow it in the dockerfile.

Ex. WORKDIR /project

6. EXPOSE:- Using this we expose the port number.

Ex. EXPOSE 80

Building an image with a tag :

$ docker build -t <image name>: <tagname> “Location of dockerfile"
Example: docker build -t myimage:2.0 .

Some other commands:

For working with Host Engine:-

$ docker -H <host_ip_address>:<2375> run <image name>

For restricting usage of CPU:
(Here .5 signifies 50% CPU usage)

$ docker run --cpus=.5 ubuntu

For restricting usage of memory:
(Here 100m signifies 100mb)

$ docker run --memory=100m ubuntu

For executing a command in a running container:

$ docker exec <container id> <command>

That’s all for the overview.
If you would like to learn more / have feedback, please let me know, so I can improve & write more for you interested folks.
Keep learning! :)

--

--

Sejal Goyal

A technology enthusiast who likes writing about Data Science.