Container and its usage

Container and Docker have been hot topic in the recent years. Today, it is unthinkable to start a new project without “dockerizing” it. But what exactly is a container? And why is so useful? Let’s try to answer these and others question on this text.

A container is a form of virtualization. By virtualization you can think of a way to use a computer’s resources more eficciently. There are two types of virtualization. The first one is to emulate a full Operational System (a Virtual Machine, VM), using a hypervisor. There are a lot of details on this topic, because the hypervisor can be either type 1 (bare metal) or type 2, for example. Examples of these type of virtualization are VirtualBox, from Oracle, or Xen. This is a well stablished solution, as this has been developing since the 1970s. However, as a new layer is introducted (the hypervisor), there is a small overhead on the overall performance.

The second type of virtualization is the container, also known as lightweight virtualization. Using containers, we remove the need of having an hypervisor. Each process runs on the same OS. The resources are limited by the host Operational Systems, using tools like Linux namespaces and Linux Control Groups. With these, a container cannot see another container’s info, isolating it.

And how is the performance of a container, when compared to hypervisor virtualization? According to this article (Performance evaluation of containers and virtual machines when running Cassandra workload concurrently - Shirinbab - 2020 - Concurrency and Computation: Practice and Experience - Wiley Online Library), who compared the performance of a Cassandra database on a non-virtualized environment, a Docker (container) and a Hypervisor virtualization, the performance of the container has almost similar to running it bare-metal (non-virtualized), being the Hypervisor the slowest on this scenario.

Today, although Docker is the most known container runtime, it is not the only. For example, you can use podman or containerd to run containers. And how exactly you execute a container? Using Images. A image is a template, it tells to the container runtime (Docker, for example) what to execute. Also, the advantage of a image is that is made of layers. Each instruction is stored as a layer. With this, if two containers uses the same layer, it can be reused, avoiding downloading them twice. Only the layer that differs will be downloaded. You can declare a image using a Dockerfile file:

FROM wordpress,

# Set environment variables used by the Wordpress image
# DB_USER and DB_PASSWORD are included as an example. However,
# these should be removed and set during docker run.
ENV WORDPRESS_DB_HOST=127.0.0.1 \
WORDPRESS_DB_USER=wpuser \
WORDPRESS_DB_PASSWORD=super-secret-password \
WORDPRESS_DB_NAME=wpsite \
WORDPRESS_TABLE_PREFIX=wp_
COPY plugins/ /var/www/html/wp-content/plugins
COPY themes/ /var/www/html/wp-content/themes
COPY uploads/ /var/www/html/wp-content/uploads
 
 
 
 
 


Despite the name of the file, you don’t have to have Docker to execute this Dockerfile. There is a association, the Open Container Initiative (OCI, Open Container Initiative - Open Container Initiative (opencontainers.org)) who defines standarts to be adopted by the industry. And one of these starndart is about images. There is a specification on how the images should be made, and the Docker supports it. So, using a Dockerfile enables you to use any other container runtime, as long as they follow the spec.

Now that we know what a container, you maybe are asking yourself what is the greatest advantage to the developer. In my opinion, is having the possibility to have the same environment that will run in production. For example, if you need a specific version of your language, like PHP 8.1, you can declare it on the Dockerfile. Assuming that your production will also run in a container, you can avoid errors of testing in one version of the language and deploying it in another. Other advantage is that you can run your dependencies as a container too. For example, there are images for Redis (redis - Official Image | Docker Hub= ), PostgreSQL (postgres - Official Image | Docker Hub ) and others. Using those images, you can start your dependencies whenever you need, without even having to install it on your machine. A very practical way to develop.

If you liked the idea of running your applications using containers, and you don’t know where to start, there are a lot of documentation online. A good way to start is reading the Docker docs (Docker Documentation: How to Build, Share, and Run | Docker Documentation ), or, if you prefer, you can watch YouTube tutorials, like this one Docker Tutorial for Beginners - A Full DevOps Course on How to Run Applications in Containers - YouTube

Written by Rodrigo Müller

Carolina Sobral