📜 ⬆️ ⬇️

Five Docker Utilities You Should Know


Image source


On the Internet, you can find many useful tools for Docker . Many of them belong to the Open Source category and are available on Github. In the past two years, I have been actively using Docker in most of my software development projects. Once you start working with Docker, you realize that it turns out to be useful for a much wider range of tasks than you originally intended. You will want to do even more with Docker, and it will not disappoint!


The Docker-community lives an active life, daily producing new useful tools. This turbulent activity is difficult to keep track of. Therefore, I decided to choose some of the most interesting and useful Docker utilities I use daily. They make work more productive by automating operations that would have to be performed manually.


Let's look at the utilities that help me in the process of dozerization of everything and everything.


1. Watchtower - automatic update of Docker containers


Watchtower monitors running containers and tracks changes in the images on the basis of which they were created. If the image has changed, Watchtower automatically restarts the container using the new image. This is useful for local development, if you want to work with the newest versions of the tools used.


The Watchtower utility also comes as a Docker image and runs in a container. To run it, enter the following command:




We launched the Watchtower with the /var/run/docker.sock mounted file. This is necessary so that the Watchtower can interact with the Docker daemon through the corresponding API. We also passed the interval option of 30 seconds, which determines the polling interval. Watchtower has other options that are described in the documentation .


Let's launch the container and re-install it using Watchtower.




Watchtower will now begin monitoring the friendlyhello container. If I place a new image on the Docker Hub, the Watchtower will detect it during the next launch. Then it will correctly stop the container and restart it from the new image. Also, the options specified on the run command will be transferred to the container, that is, it will be launched with -p 4000: 80 .


By default, Watchtower will look for new versions of images in the Docker Hub registry. In this case, the Watchtower can poll closed registries using credentials from the environment variables REPO_USER and REPO_PASS to log in.


More information about the Watchtower can be found in the documentation .


2. Docker-gc - removal of unnecessary containers and images


The docker-gc utility helps to clean the Docker-host by removing the images and containers that are no longer needed. Removed containers completed more than an hour ago, as well as images that do not belong to any of the remaining containers.


Docker-gc can run as a script or container. We will use the second option. Run docker-gc to find out which containers and images can be deleted.



')

So that docker-gc can interact with Docker through its API, we have mounted the Docker socket file. To search for containers and images that can be deleted, run docker-gc with the environment variable DRY_RUN = 1. No changes on the disk will be made. It is always better to first ensure that docker-gc is not going to remove anything extra. Here is the output of this command:




If you agree with the cleanup plan proposed by docker-gc, run the same command, but now without DRY_RUN.



The output will list the deleted docker-gc containers and images.


Docker-gc has a few more options. To get more information about this utility, I recommend reading the relevant documentation .


3. Docker-slim - a magic pill that will help your containers lose weight.


Concerned about the size of your Docker images? Docker-slim will help you!


This utility uses static and dynamic analysis to create lightweight versions of Docker images. To work with docker-slim, download the binary from Github (versions for Linux and Mac are available), and then add the path to the executable file to the PATH environment variable.


Using the instructions from the official Docker documentation, I created an image for the friendlyhello demo application. The size of the image was 194 MB.




For a very simple application, we had to download 194 MB! Let's see how many megabytes you can throw off with docker-slim.




Inspecting the original image, docker-slim will perform several steps and then create a light version of it. Let's see what size the result was:




The illustration shows that the image size has decreased to 24.9 MB. If you run the container, it will work in the same way as its previous version. Docker-slim does a good job with Java, Python, Ruby, and Node.js applications.


Try docker-slim yourself, and maybe this will allow you to free up a lot of space. This utility worked correctly in almost all my projects. Get more information about docker-slim from the relevant documentation .


4. Rocker - overcoming Dockerfile limitations


Most developers prefer to use Dockerfile to build images. Dockerfile is a declarative way to define image creation commands that a user could execute on the command line.


Rocker adds a set of new instructions to the Dockerfile. Rocker was created in Grammarly to solve the problems this company faced when using Dockerfile. Grammarly wrote an informative article explaining why Rocker was created. If you have a desire to better understand Rocker, I recommend this article to be read. It noted two problems:


  1. The size of the images.
  2. Assembly speed

There are also a few Rocker instructions added. For information on all supported Rocker instructions, see the documentation .


  1. MOUNT - allows you to customize the sharing of volumes in different assemblies, which is convenient, for example, for dependency management tools.
  2. FROM - this instruction exists in the standard Dockerfile. At the same time, Rocker allows you to add several FROMs to a single file. This means that using one Rockerfile you can make several images, for example, one for the assembly, and another for the execution of the application. The first set of instructions will collect the artifact using all the necessary dependencies. The second set of instructions can use the artifact created in the first step. With this technique, a significant reduction in the size of the image is achieved.
  3. TAG allows you to create image labels at different stages of the assembly, that is, you no longer need to do this manually.
  4. PUSH is used to load images into the registry.
  5. ATTACH allows you to interactively connect to the container at intermediate stages of assembly, which is very convenient for debugging.

Rocker requires installation. For Mac users, the commands are as follows:




Rockerfile looks like this:




To build an image and upload it to Docker Hub, do:




Rocker has a very interesting functionality. To learn more, refer to its documentation .


5. Ctop is a top-like utility for displaying information about containers.


I started using ctop relatively recently. This utility displays real-time metrics of several containers at once. If you are using a Mac, you can use the following command to install ctop:




For ctop to work, the environment variable DOCKER_HOST must be set.


Run ctop to display the status of all containers.




To display information only about running containers, run ctop -a .


Ctop is easy to use and very useful. More information can be found in the documentation .


References:


  1. Original: 5 Docker Utilities You Should Know .

Source: https://habr.com/ru/post/330326/


All Articles