Modern infrastructure does not exist without microservices and containers. These technologies are great when it comes to running an application in processes isolated from resources. Docker has become a household name when it comes to containerization management. It's an application that makes it easy to manage application processes in containers.
What is a container? It is a structure that contains applications with all the required libraries and dependencies. This makes it possible to port the needed packages to other distributions without having to create a similar environment. Unlike hardware virtualization, containers are easy to port and require fewer resources. Most importantly, containers are completely independent of external distribution.
In this article, we will explain how Docker Community Edition works using Ubuntu 20.04 as an example. We will describe how to work with containers and images using concrete examples and show what Docker Compose is.
As for hardware recommendations, Docker has no hardware requirements. Considering that it is a virtualization utility, it all depends on the scale of applications that will be deployed inside the containers. There is one requirement for the "machine" – virtualization support.
Before proceeding with the installation, we suggest that you check if your environment meets the minimum system requirements:
Earlier it was stated that we would use the Ubuntu server to set up Docker.
We need the latest version of the utility. You don't have to check the Ubuntu repository because they don't always post the latest release of the software.
We suggest that you go straight to the official Docker registry. This requires adding a new batch source. To make sure that working files will be loaded, you will need to load the GPG key. After the manipulation is done, you can install the package.
First, update the list of packages on the device:
$ sudo apt update
Next, you will need to install software designed to make apt work properly over HTTPS:
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
Optionally, additional kernel software may be needed. This is necessary to enable the aufs file system to work with the utility containers. This way you can quickly detect changes and create Snapshots. These are read-only images.
$ sudo apt install linux-image-extra-$(uname -r) linux-image-extra-virtual
Add a GPG key for the Docker repository:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
The following three commands are necessary to add the software repository directly to Ubuntu and then install Docker into the distribution.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
$ sudo apt update && apt-cache policy docker-ce
$ sudo apt install -y docker-ce
In some cases, you may not be able to connect to a socket when starting Docker. To avoid this, add the user to the Docker group. Use the following command:
$ sudo usermod -aG docker $(whoami)
After these actions, the software will be installed, launched, and added to the autostart. You can additionally check the functionality with the following command:
$ sudo systemctl status docker
If daemon works correctly, a similar message will appear on the line:
$Output● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-05-19 17:00:41 UTC; 17s ago
TriggeredBy: ● dockersocket
Main PID: 24321 (dockerd)
└─24321 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
At this point, the installation is complete and the utility is ready to go. To fully use Docker, you will need an auxiliary tool called Docker Compose.
What is this software? Compose is a tool that helps you run several different containers. The utility uses service definitions to create fully customizable multi-container environments. They can share the network and external memory space. Run the following command sequence:
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
This command sets the correct permissions to execute docker-compose commands:
$ sudo chmod +x /usr/local/bin/docker-compose
Check that the software is up to date and confirm a successful installation:
$ docker-compose --version
If we see such a conclusion:
docker-compose version 1.29.2, build 5becea4c
So the process was successful.
The docker command is used with options and subcommands with additional arguments. Let's look at the schematic syntax:
docker [options] [command] [arguments]
You can view the table of all teams using the command:
The table will open:
It is not possible to use the container without the downloaded Docker Image. If no changes have been made, the utility takes the image from the official hub. It is called Docker Hub. It contains all the images that are supported by the developers of the utility. Everyone can place images in the Hub registry. Therefore, popular applications, Linux distributions, and applets already have prepared images in the repository.
Let's enter a command to check if Docker Image can be loaded into the machine's internal memory:
$ docker run hello-world
First, the utility will check if the specified image is in memory. Since Docker will not detect anything, it will download the required image and then run it in the container:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
This message indicates that the process of downloading and starting the desired image works without errors.
To find the desired Docker Image in the Hub, use the search subcommand and the name of the desired image. The script will start searching the content of the Docker Hub, showing all the results that match the given parameters. Pay attention to the OFFICIAL column. If there is an OK line next to the image in this column, it means you are facing an official image from the founding team and not a fork.
To download it, just type the pull subcommand. Let's assume this is an Ubuntu image. In this case we type:
$ docker pull ubuntu
This conclusion indicates that the process was successful:
Using default tag: latest
latest: Pulling from library/ubuntu
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
97f170c87c6f: Pull complete
Status: Downloaded newer image for ubuntu:latest
The table with the installed images can be viewed with the command:
$ docker images
Running a container command shell
The RUN command is used to start the container. Let's take a look at the options that can be used with RUN:
Use the command:
$ docker run -it ubuntu
We run the Ubuntu image with access to the command processor in interactive mode. After that we go to the container environment, the console will display its ID. We will need this information later when we need to specify the container to uninstall.
All the usual commands are available inside the container. Considering that the image does not contain most of the files you need. Therefore, you will need to update the repository:
$ apt-get update
Note that the sudo command is not used. Since the work is done as a user with superuser rights, there is no need to type the sudo command. Let's install a simple Dialog utility:
$ apt-get install -y dialog
Any action is available inside the container, you can create files, install applications, or delete data – all actions will be relevant to the running container. When the container is deleted, all changes will also disappear. If you want to keep the settings, use the commit command. This will create a modified container. To get started we need a list of running containers:
$ docker ps
Here we need an identifier, run this command:
$ docker commit -m "what changes were added" -a "author name" container_id repository/new image name
When the process is finished, the modified container will be saved in memory. To find it, let's run the image list:
Let's take a look at the list of commands to control:
You don't have to specify an identifier for the command to work correctly. You can use the name of the container, it is available in a separate column
$ docker start d9b100f2f636
$ docker start sharp_volhard
The container list command allows you to run options:
Docker is the main utility for starting and managing containers. It is easy enough to learn, a novice user can easily understand all the features. Docker significantly speeds up development, makes it easier to move the system from one server to another, and allows you to work in a secure environment.