Code

What is Docker and how does it work?

What is Docker and how does it work?

Practical course: “Profession DevOps engineer”

Learn more

The technical leader is passionate about sports, traveling, and computer games. He professionally creates technical articles and shares his knowledge through technical threads on Twitter. His experience and expertise in technology make him a valuable resource to the community.

Links play a vital role in web content, providing navigation and access to additional information. They help users find the resources they need and improve the site's user experience. It is important to format links correctly, using keywords in the anchor text, to improve the page's SEO ranking. Link optimization helps increase website visibility in search engines and improves user experience. Implementing internal and external links allows you to create more structured content and increase its value to visitors. Use links wisely to direct users to important information and increase their time on the site.

Containerization is an innovative concept in information technology. A common analogy is that developers package their applications and dependencies into a single container, similar to cargo being transported at a port. This metaphor is quite appropriate, as a digital container can be easily transferred and deployed across various environments, becoming a fully functional product. Containerization simplifies the process of developing, testing, and deploying software, while providing greater flexibility and scalability. With containers, developers can create isolated environments, eliminating issues related to incompatibility and configuration. This approach significantly increases team efficiency and speeds time to market.

Docker is the primary containerization tool used to build internal service infrastructure. It is actively used in conjunction with orchestrators such as Kubernetes and OpenShift. These technologies mark the transition from traditional virtual machines to cloud infrastructure. Using Docker in combination with orchestrators allows for more efficient and dynamic resource management, ensuring the flexibility and speed of application deployment.

What is Docker

Docker is a powerful platform for packaging applications into containers that contain all the necessary dependencies and environment. This makes it easy to port and run applications on various systems without the need for complex configuration. With containerization, developers can maintain consistency across development, testing, and production environments, significantly simplifying the process of deploying and managing applications. Using Docker helps streamline workflows and increase the efficiency of software development.

A containerized application is isolated from the operating system and other applications, eliminating the need for developers to worry about the runtime environment. Operations engineers can run applications in a unified environment, minimizing issues with system dependencies. This isolation contributes to increased application stability and portability, making containerization an essential tool for modern developers and IT professionals.

Docker was developed in 2008 as an internal project at dotCloud, which later changed its name to Docker Inc. In 2013, dotCloud took a significant step by open-sourcing Docker to the developer community. This allowed Docker to quickly gain popularity, becoming a key tool for application containerization. The platform's openness and flexibility have fueled the development of an ecosystem around Docker, including a variety of tools and technologies that significantly simplify the process of developing, testing, and deploying software.

Early versions of Docker functioned as an enhanced wrapper for LXC, but since 2015, it has used its own library, libcontainer, which abstracts the virtualization capabilities of the Linux kernel. This was a key step in transforming Docker into an independent technology. The transition to libcontainer was no accident, as the flexibility and manageability of LXC containers did not meet the needs of developers. Docker offered a more efficient containerization solution, which contributed to its popularity and widespread adoption in modern DevOps practices.

Docker continues to gain popularity thanks to the support of an active community. This platform has become a key element in the DevOps adoption process, enabling fast delivery pipelines and workflow automation. By simplifying application deployment and container management, Docker meets modern software development and operations requirements, making it an indispensable tool for many companies.

Docker Structure and Operating Principle

Virtualization in Docker is performed at the operating system level. The virtual environment is launched directly from the host OS kernel, allowing for efficient use of its resources. Thanks to this approach, Docker provides high performance and ease of container deployment, making it a popular choice for developing and deploying applications in the cloud.

Docker is supplied with key components that ensure its functionality and efficiency. The main elements are the Docker Engine, which is responsible for creating and managing containers, and the Docker CLI, which provides a command interface for interacting with the platform. Also included are Docker Compose for simplifying the management of multi-container applications and Docker Hub, which is used to store and share container images. These components enable developers to efficiently develop, test, and deploy applications in a containerized environment.

  • Docker host is the operating system on which Docker is installed and runs.
  • Docker daemon is a service that manages Docker objects: networks, storage, images, and containers.
  • Docker client is a command-line client that users use to interact with the Docker daemon, send commands to it, create and manage containers.
  • Docker image is an immutable image from which a container is deployed.
  • Docker container is a deployed and running application.
  • Docker Registry is a repository where images are stored.
  • Dockerfile is an instruction file for building an image.
  • Docker Compose is a tool for managing multiple containers. It allows you to create containers and configure them.
  • Docker Desktop is a GUI client distributed under the GPL. The free version runs on Windows, macOS, and recently on Linux. This is a very convenient client that displays all Docker entities and allows you to run single-node Kubernetes on your computer.
Screenshot: Skillbox Media

Docker was originally developed for the Linux operating system. For this reason, Docker runs on Linux virtual machines on both Windows and macOS. macOS uses VirtualBox for this purpose, while Windows uses Hyper-V. This allows users of both platforms to leverage Docker's capabilities, ensuring compatibility and performance when containerizing applications.

Docker architecture diagramsScreenshot: Skillbox Media

Working with virtual machines increases resource consumption, which negatively affects the performance of Docker on macOS and Windows. This may be acceptable for development, but in a production environment, this approach is not justified. In most cases, Linux is chosen for deploying applications and services in production, as this operating system provides better performance and stability for Docker.

How is virtualization different from containerization?

Containers and virtual machines are different approaches to virtualization. Virtual machines (VMs) perform virtualization at the hardware level, which provides isolation and the ability to run multiple operating systems on a single physical server. In contrast, containers like Docker perform virtualization at the operating system level, which allows applications to run in isolated environments using a common OS. This makes containers more lightweight and efficient than VMs, as they require fewer resources and less time to deploy. The choice between containers and virtual machines depends on specific tasks and infrastructure requirements.

A virtual machine is an independent computer with unique hardware and an operating system. A common practice is to purchase a powerful server to install a hypervisor, which serves as the basis for creating virtual machines. The server is divided into several virtual computers, which eliminates the additional costs of purchasing separate physical machines. Using virtual machines optimizes resources, simplifies management, and increases the flexibility of the IT infrastructure.

Virtual computers are full-fledged systems capable of supporting any operating system. On such machines, it is possible to work through a graphical interface in multi-user mode, as well as install and run a variety of applications and services. Using virtual computers allows for efficient resource optimization, security, and flexibility in IT infrastructure management.

While the goal of a virtual machine is to completely replicate a computer's hardware, the main task of Docker is to create an environment for a single application. Docker containers run inside the operating system, without the need for hardware virtualization, which allows them to use resources directly through the OS. This makes Docker containers lightweight, faster to deploy, easier to scale, and smaller in size. This makes Docker the optimal solution for developing and deploying applications in today's environment.

Creating a separate virtual machine for an application, including installing and configuring the operating system and managing permissions, can be prohibitively expensive. In most cases, a lightweight environment to run the application is sufficient. In this context, a container is an ideal solution because it allows you to isolate and manage a single primary application while providing the necessary flexibility and resource efficiency. Containerization simplifies application deployment, reduces infrastructure costs, and speeds up development and testing.

Both methods isolate the application from the primary operating system, but virtual machines allow you to install any operating system, while Docker is primarily aimed at using the Linux kernel. Recently, the ability to run Windows in Docker has become available, but I have not yet had the opportunity to test this functionality.

Docker and virtual machines have their own characteristics that do not always play well together. In some cases, a production server is split into virtual machines, which then run containers. However, this dual-virtualization approach can lead to excessive resource consumption, which has been criticized. If a company already uses this practice, it's worth considering switching to Kubernetes. This platform allows applications to be installed directly on physical hardware, which can significantly improve resource efficiency and simplify container management.

Large companies typically use virtualization, where virtual machines are deployed on physical servers in data centers. Infrastructure engineers create virtual machines and build the necessary infrastructure on them. Implementing orchestrators streamlines processes by eliminating redundant management layers and simplifying resource deployment and scaling. This leads to increased efficiency and reduced IT infrastructure maintenance costs.

If you have sufficient resources, it is recommended to install Docker on a virtual machine. This will isolate applications from each other, ensuring their secure and efficient interaction. This approach helps optimize operations and manage dependencies, making application development and deployment more manageable and predictable.

Docker Entities

Docker interacts with various entities that allow you to effectively manage containers and applications. The main components of Docker include images, containers, networks, and volumes. Images are templates for creating containers, and containers are executable instances of these images. Networks enable communication between containers, and volumes store data that can be used by containers. Understanding these entities is key to optimizing the Docker experience and improving application performance.

A Docker image is a template from which containers are created. It can be compared to a layer cake: we add a filesystem layer on top of the base image, resulting in an immutable image. This image can be used to install an application, along with the necessary configurations and dependencies. Additionally, Docker images can be inherited, allowing you to create new images by adding layers of files and storing them as immutable images. This provides flexibility and repeatability in application deployment.

Screenshot: Skillbox Media

A Dockerfile is an important element in the process of creating Docker images. If you compare a Docker image to a pie, then a Dockerfile can be considered a recipe for baking it. This file contains key instructions for building an image, including selecting the base image, specifying sources and file destinations, and other necessary commands for setting up the environment. Properly formatted Dockerfiles enable efficient build automation and simplify the deployment of applications in containers.

A container is a runtime entity created from an image and is an application deployed using Docker. In this context, an analogy can be drawn: an image functions as a software installer, while a container represents a running application. Docker containers isolate applications and their dependencies, allowing for efficient resource management and consistent operation across different environments.

ContainersScreenshot: Skillbox Media
ContainersScreenshot: Skillbox Media

When deploying a file system-based container, an additional mutable layer is created. An application running inside the container can write and edit data in this layer. After deleting the container, all data in the mutable layer disappears. However, to save data, it is possible to use volumes, which ensure persistence and protection of information even after the container is deleted. This allows for efficient data management and guarantees its safety in the context of dynamic work with containers.

Docker Registry is a repository designed for storing Docker images. It can be either local or public. Repositories are created on platforms such as Docker Hub and GitLab, where users can post images with detailed descriptions, as well as different versions and tags. Docker Registry provides convenient image management, versioning, and availability for developers, which simplifies the process of deploying applications and microservices.

How Docker Helps in Practice

Docker is a powerful tool for containerizing applications, and its benefits can be demonstrated through several use cases.

The first example is application development and testing. Docker allows developers to quickly create isolated environments, which simplifies the testing process. Using containers, you can ensure that the application will behave consistently at all stages, from development to production.

The second example is application deployment. With Docker, you can easily deploy applications to various environments, such as on-premises servers, cloud platforms, or servers. This contributes to faster and more efficient software delivery.

The third example is microservices architecture. Docker is ideal for implementing microservices, as it allows each service to run in a separate container. This simplifies scaling and managing dependencies between services.

The fourth example is resource management. Docker helps optimize resource usage by allowing multiple containers to run on a single server without conflicts. This leads to more efficient use of server capacity and reduced infrastructure costs.

These examples demonstrate how Docker improves the processes of developing, deploying, and managing applications, making them more flexible and efficient. Docker continues to gain popularity thanks to its ability to make life easier for developers and system administrators.

To install libraries or databases, developers often need to follow the instructions on the official website. The process involves downloading, installing, configuring, and running. Switching to a different dependency requires removing the current one, which creates additional complexity. This approach requires significant time and effort to manage each dependency.

Docker offers an effective solution for developers and system administrators. Every day, library, framework, and database vendors upload their software products to Docker Hub in Docker image format. These images can be easily downloaded and deployed using Docker, allowing you to quickly get started with the software you need. When finished, you can save your changes, push them back to the repository, or completely remove the image without leaving a trace in the operating system. This makes Docker an ideal tool for testing and development, providing a clean and manageable environment.

Docker's unified management interface simplifies the workflow by eliminating the need to learn numerous individual commands. Users only need to learn basic Docker commands for downloading images, starting containers, forwarding, stopping, and removing ports. Docker allows you to run an unlimited number of identical databases within a single operating system. Thanks to the isolation mechanism, any errors that occur do not affect the main operating system, ensuring its stability and protection from possible failures. Using Docker significantly simplifies application management and improves their security.

To successfully run automated tests, certain dependencies, such as databases and message brokers, must be installed and configured. These components must be pre-configured on the build server. However, this step can be challenging: overlooking even one detail in the configuration can lead to data corruption or other issues. A safer approach is to automatically deploy dependencies as containers directly to the server. This not only speeds up the test execution process but also allows you to completely remove the containers after testing is complete, minimizing the risk of negative impact on the system.

Tests may corrupt certain data, but it will be removed along with the container. Using Docker to run automated tests allows you to create a universal server. Thanks to containerization, you can run various applications and services on this server. This not only simplifies environment management but also helps save on hardware resources and system configuration.

Once testing is complete, the project is packaged into an image, which is then published and distributed to clients or infrastructure engineers. This process ensures reliable delivery and easy deployment of software, which minimizes risks and accelerates product adoption.

Docker significantly simplifies the application deployment process. Reliability and maintenance specialists (SREs) don't need to worry about which dependencies need to be installed, since everything is already built in a container. For them, it's a kind of black box that they can update consistently and automatically using the same commands. This helps minimize errors and speed up the deployment process, which is especially important in a rapidly changing development environment.

Modern deployment environments are becoming more versatile thanks to containers. You can deploy one container in the same environment today and another tomorrow. It's important to note that containers can host significantly different applications. This allows for greater flexibility and efficiency in application development and deployment processes, and simplifies dependency and resource management. Using containers in deployment ensures a stable and predictable environment, which is an important aspect for modern developers.

Disadvantages of Docker

Docker offers many advantages, but using its capabilities comes at a cost. The platform requires resources for installation, configuration, and maintenance, which can impact a project's budget. However, investing in Docker can significantly improve productivity and simplify the development process. Effective container management and scalability make Docker an indispensable tool for modern programming.

Docker requires significant resources, as it creates an additional logical layer, which can lead to increased consumption of system resources. Therefore, it is important to carefully evaluate what is more important to you: resources or ease of use. If you have sufficient resources, using Docker is an excellent solution for conveniently updating and versioning applications, minimizing the risk of damaging the operating system. In case of limited resources, it is worth considering the classic application installation scheme, which will be more efficient.

For large-scale applications, an orchestrator is necessary. Docker is ideal for running multiple containers. The standard version of Docker Compose includes a mechanism for managing container launches using a YAML configuration file. However, this mechanism is quite simple and will not cope with applications consisting of 50-100 services. Docker does not have the necessary tools for resource management and allocation, reservations, and fault tolerance, which makes it difficult to implement various container update schemes. In such cases, it is better to consider more advanced orchestration tools, such as Kubernetes, which offer advanced container management features and allow for efficient application scaling.

Large applications with microservice architectures often use orchestrators such as Kubernetes and OpenShift. In Kubernetes, the primary management element is the Docker container. However, almost no one uses pure Docker in production. Previously, there were companies in Russia that shared their negative experiences with Docker at conferences. Switching to orchestrators simplifies container management, automating deployment, scaling, and application management.

Kubernetes provides a powerful abstraction layer over physical hardware and is an alternative to hypervisors for virtual machines. This container management system allows you to configure security policies, implement various update schemes, and efficiently manage resources. Thanks to its flexibility, Kubernetes simplifies application deployment and scaling, ensuring stability and reliability in cloud and on-premises environments.

Issues installing Docker on Windows and macOS. Docker is originally designed for Linux, which leads to certain limitations when running on other operating systems. In particular, certain network types may not be supported. While many users may not encounter this limitation, it is important to keep it in mind. In addition, conflicts with VirtualBox may occur on Windows during the Docker installation, which should also be taken into account when planning to work with this platform.

How to learn Docker

It is recommended to learn Docker using books and online resources, as well as consolidate the acquired knowledge in practice. Hands-on experience will help you better understand the basic concepts and capabilities of Docker, which will significantly improve your skills in the field of containerization and DevOps.

Books about Docker. We recommend paying attention to two useful books: "Using Docker" and "Docker in Action". Although they were published in 2018 and 2019, which makes them a little outdated, they still contain a lot of valuable information. However, it is worth considering that some aspects may not be relevant. We expect to release new editions of these publications soon, taking into account the latest changes and innovations in the Docker ecosystem.

The docker.com website is the primary resource for Docker users. It provides references and documentation for Docker, Dockerfiles, images, and Docker Compose. You can use this site to check the relevance of commands, explore examples, and gain the knowledge you need to use Docker effectively. It is the ideal place for developers and system administrators looking to deepen their skills in containerization and application management.

Exercises. I conduct training sessions and internal training sessions for teams. I most often recommend the following exercises to improve teamwork and collaboration.

  • Install Docker on your work computer. Get a ready-made Docker image from Docker Hub with a database and run a container based on it. You can do this by following the instructions that the image manufacturer posts on Docker Hub. Then connect to the running database with a client and make sure everything works.
  • Write a simple REST API service and a Dockerfile for packaging the service into an image, looking at references or a book. Your task is to understand the theory, obtain a Docker image, and add tags.
  • From the image obtained earlier, create and run a container. You will need to understand the Docker run command parameters, port settings, passing variables to the environment, mounting, and basic parameters. You should also learn how to connect to the container, execute bash commands, and view application logs.
  • Complexify the test service by teaching it to work with a database. Then deploy containers with the service and database and connect them with an internal network. A simple yet very useful exercise, which often causes difficulties in interviews.
  • Moving on to Docker Compose. Learn how to write YAML and create a container group, how to connect them to a network, and work with mounts.
  • Publish your Docker image to Docker Hub. You can only publish one image per account for free, but GitLab has no such restrictions. However, it's a bit more complicated, and a beginner might get confused. You can also create a private registry in GitLab and publish projects without restrictions.

Read also:

  • All about Kubernetes: containers, orchestration, tooling, virtual machines, competitors, and the ecosystem
  • "I had no experience, but I wanted to." Test, probationary - and I'm an IT analyst at a bank"
  • Docker Practice: Getting to Know the Docker-Maven-Plugin