Blog ENG

Introduction to Docker

Robert Jambrečić

In this blog, we will introduce to you the Docker containerization platform. We will explain the difference between a container and a virtual machine (VM) and list some advantages and disadvantages. We will then walk you through the basic commands and finally run a simple Django app inside the Docker container.

Containers vs virtual machines

Containers and virtual machines are packaged computing environments that combine different IT components and isolate them from the rest of the system. Containers are created on top of the operating system (OS) core and contain only the most important files needed for the system to work. On the other hand, each virtual machine uses its own operating system [1]. For this reason, containers are most often measured in megabytes while VMs are measured in gigabytes. For VMs, the so-called “Hypervisor” takes care of the disposal of physical resources such as CPU and RAM. For the same thing, Docker uses the Docker engine.

Figure 1. VM and container architecture

The starting speed is one of the advantages of using a container. It takes a few minutes to start the VM, while the containers start in just a few seconds. On the other hand, VMs are completely isolated from one another and are therefore safer.

Docker images and containers

When using Docker, you will often come across the following two terms: images and containers. A Docker image is a template that comes with instructions for setting up the container.

An image consists of a set of files (or layers) that contain all the necessary elements, such as dependencies, source code, and libraries needed to set up a fully functional container environment. A Docker container is a virtualized environment that allows you to separate application execution from the underlying system. The container is one instance of the image [2].

Docker basic commands

Prerequisites for further active following of this blog is having Docker installed. If you haven’t already, you can do so by following the instructions on the official website get-docker.

You can check the success of the installation by running the following command inside the terminal:

docker run hello-world

In the first line after running the command, you can see how Docker first locally searches for the “hello-world” image. As it did not find the image, Docker searched for the image in the Docker registry and retrieved it from there.

Using the pull command, we can download Docker images from the Docker registry and save them locally to our computer. With the following command we can download the “alpine” image (the smallest Docker image based on Alpine Linux):

docker pull alpine

To create a container from an image, run the following command:

docker run -it alpine

You are currently inside the container and by running the command ls you can get a list of files in the current directory inside the container. You can type exit to get out of the container.

The docker ps command displays all active containers. As there are currently no active containers, the following printout is obtained.

By using the command docker ps -a we can get all the containers, both active and those that have finished working (column “STATUS” states whether the container is active).

To delete a container, the following command is used docker rm container_name (e.g.

docker rm youthful_mcclintock).

If we want to assign a name to the container, we can do so with a flag –name :

docker run –name alpinko -it alpine

We can then activate or turn off the container named “alpinko” using the following commands:

docker start alpinko

docker stop alpinko

And in the end we can delete it:

docker rm alpinko

Django application within the Docker container

Once you’ve mastered the basics of Docker, it’s time to create your own Docker image.

Before starting, you need to download our simple Django application from the Git repository and position yourself in the repository:

git clone https://github.com/robijam/hello.git

cd hello/

Inside the repository, you can find a Dockerfile. In this example, the Dockerfile specifies everything our container needs to contain for the Django application to run inside it.

Figure 2. Dockerfile

The FROM command defines an existing Docker image, which will be used as the base image.

The ENV command defines environment variables.

The WORKDIR command defines our working directory within the container.

The COPY command is used to copy our local files to the container.

The RUN command executes the instruction in the image.

The following command creates a Docker image using Dockerfile:

docker build -t django-image .

Flag -t , assigns a name to the image. “.” indicates that we are creating an image from the Dockerfile located in the current directory.

From the “django-image” we created, we can create a container and run a Django server inside it:

docker run -p 8000:8000 -v $(pwd):/you_can_do_it -it django-image bash

python manage.py runserver 0.0.0.0:8000

-p is used for port forwarding, i.e. all requests that arrive locally on port 8000 will be forwarded to the container on port 8000

-v is is used for sharing local files with the container

If you enter the following address into your browser 127.0.0.1:8000, you should get the following page:

We hope that you have successfully reached this part and that you have received our kind regards 🙂

Where does our team use Docker?

We will try to briefly describe the architecture of our system for the detection and classification of products on the shelves and explain the benefits of using Docker. Our system uses several services: the Django application, database, AI model service (e.g. TensorFlow Serving or TorchServe), asynchronous task service (e.g. Celery) and several other services. We are also developing the system on several servers and locally on our computers. If we were to set up all these services manually on all computers (and then set up different environment variables …), we would lose a lot of time. The Docker registry contains the vast majority of images (Django, PostgreSQL, TensorFlow Serving …) where everything is already set up, and it’s up to us just to download them, create containers from them and use them. This saves time on the first system configuration as well as each time we move the system to another computer.

Conclusion

Docker is a containerization platform. Containers are created on top of the operating system kernel and contain only the most important files needed to run the system. An image is a template that comes with instructions for setting up the container. The main advantages of Docker are launch speed, scalability and portability. Using Docker to build complex systems will greatly speed up and facilitate the development of your system.

References:

[1] Containers vs. virtual machines, https://docs.microsoft.com/en-us/virtualization/windowscontainers/about/containers-vs-vm

[2] Comparing Docker Images To Docker Containers, https://www.whitesourcesoftware.com/free-developer-tools/blog/docker-images-vs-docker-containers/