Why Docker?

Muhammad Oktoluqman Fakhrianto
4 min readJun 7, 2021

--

Last year when I was coding back-end server for a website, I want to try to run the back-end server as close how it actually run in the production server. But to run it like that, I need to install docker. The question “Why do I need Docker?” and “Why in every CI/CD use Docker?” pop-up in my mind multiple times. To answer these questions, first we need to understand Docker.

What is Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

source: Docker Overview

Docker provides the ability to package and run an application in a loosely isolated environment called a container. Because it is isolated, you can run multiple containers at the same time. Containers are also lightweight because they don’t need programs that are currently installed on the host machine. It makes it easy to share containers and be sure that everyone with different machines can run it without issue.

How does Docker work?

source: https://docs.docker.com/get-started/overview/#docker-architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface. Another Docker client is Docker Compose, that lets you work with applications consisting of a set of containers.

source: Docker Architecture

Components

  • Docker Daemon is a daemon that listens to Docker API requests and manage Docker objects such as images and containers. In my Linux machine, I have to run sudo systemctl start docker.service to run the docker daemon.
  • Docker Client is the main command to interact with Docker. It is the Command Line Interface docker.
  • Docker Registries store docker images. The default Docker registry is Docker Hub public registry.
  • Docker Image is a read-only template with instructions for creating a Docker container. To get a list of images in the host machine, run the command docker images .
  • Docker Container is a runnable instance of an image. You can start, stop, move , or delete a container with Docker CLI. To run an image, we use the command docker run <args> . By default, a container is isolated from other containers and its host machine.

Example

Let’s say we want to build a Docker image after creating a web app using npm.

First create a file named .dockerignore with this content:

.git
.gitignore
node_modules
npm-debug.log
Dockerfile*
docker-compose*
README.md
LICENSE
.vscode

.dockerignore file is being used to prevent copying unnecessary files when building the image.

Then create a file named Dockerfile with this content:

FROM node:10-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Dockerfile is like a shell script for building a Docker image. Dockerfile usually starts from a base image. In this case, we use node:10-alpine for the base image. The base image could be Ubuntu, MySQL, Flutter, etc. We expose port 3000 to inform Docker which port the container should listen at runtime. Finally we run the command that we want to start, which is npm start .

After Dockerfile is created, we build the image with:

docker build .

To run the image, use the following command: (You need to replace yourimageid with the built image id from the list of images in your host machine. To get the list of images, run docker images )

docker run -p80:3000 yourimageid

We supplied -p argument to specify what port on the host machine to map the port the app is listening on in the container. Now we can access the app from your browser on https://localhost.

That’s the end of creating your own image. If you want to get an image that someone already created on Docker Hub, use this command:

docker pull dockerhubuser/imagename

What is Docker Orchestration?

Tools to manage, scale, and maintain containerized applications are called orchestrators, and the most common examples of these are Kubernetes and Docker Swarm. Development environment deployments of both of these orchestrators are provided by Docker Desktop, which we’ll use throughout this guide to create our first orchestrated, containerized application.

source: Docker Orchestration

Unfortunately I never use Docker Orchestration because the projects I’ve collaborated with didn’t require multiple containers. Most of my projects only require 1 container for testing or deployment. I usually use Docker for running CI/CD scripts in a Gitlab pipeline.

Docker is useful because running a program inside a Docker container is well isolated, as if you are running a program inside a new machine. Docker provides you with a consistent and isolated environment, which can help to reduce risks such as downtime and so on because each container only allowed to access the assigned resources. Docker is preferable in CI/CD workflow because it is lightweight and provides a standardized working environment for the developers. Docker images can be very small which could save a lot of time.

Thank you for reading. I hope you learn new things about Docker.

References

--

--