5 Docker Utilities You Should Know
5 Docker Utilities You Should Know
The Docker community has created many open source utilities, making it helpful with even more use cases than you might imagine. Check them out here.
Join the DZone community and get the full member experience.Join For Free
SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.
You can find a lot of cool Docker utilities on the web. Most of these are open source and available on Github. Over the last two years, I have become quite active with Docker, using it for most of my development projects. As you start using Docker, you will find it is suitable for more use cases than you may have initially envisioned. You will want Docker to do a little more for you—and it will not disappoint!
The Docker community is very active, with a lot of useful utilities popping up daily. It is difficult to keep check of all the innovation happening in the community. To help you, I have collected some interesting and useful Docker utilities, which I use in my daily work. These utilities make me more productive, eliminating what otherwise would have been manual work.
Let’s go through each of the utilities I find useful in my journey to Dockerize stuff.
1. watchtower: Automatically Update Docker Containers
Watchtower monitors running containers and watches for changes to the images those containers were originally started from. When Watchtower detects that an image has changed, it automatically restarts the container using the new image. I use it in my local development where I would like to try out the latest built image.
Watchtower is itself packaged as a Docker image so you can run it just the way you would run any other container. To run Watchtower, you would run the following command:
In the command above, we started Watchtower container with a mounted file
/var/run/docker.sock . This is required so that Watchtower can interact with Docker daemon API. We passed an option
interval of 30 seconds. This option defines the Watchtower poll interval. Watchtower supports a few more options, which you can use as described in their documentation.
Let’s now start a container that Watchtower can monitor.
Now, Watchtower will start monitoring
friendlyhello container. When I push the new image to Docker Hub, Watchtower, in its next run, will detect that a new image is available. It will gracefully stop the container and start the container using the new image. It will pass the options that we passed to the run command. In other words, the container will be started with
4000:80 publish ports option.
By default, Watchtower will poll the Docker Hub registry to look for updated images. You can configure Watchtower to poll private registry by passing the registry credentials in environment variables REPO_USER and REPO_PASS.
To learn more about Watchtower, I recommend reviewing the Watchtower documentation.
2. docker-gc: Garbage Collection of Container and Images
The docker-gc utility helps clean up your Docker host by removing containers and images that are not required. It removes all the containers that existed more than an hour ago. Also, it removes images that don’t belong to any remaining containers.
You can use docker-gc both as a script and container. We will run docker-gc as a container. Let’s use docker-gc to find all the containers and images that can be removed.
In the command shown above, we mounted Docker socket file so that docker-gc can interact with Docker API. We passed an environment variable DRY_RUN=1 to find which containers and images will be removed. If we don’t provide this option, docker-gc will remove all of them. It is good to first verify everything docker-gc will clean. The output of the above command appears below.
If you are fine with the docker-gc clean up plan, you can again run docker-gc without DRY_RUN flag to perform the clean up.
The output of the above command will tell you all the images and the containers that docker-gc removed.
There are few more options that docker-gc supports. I recommend you read the docker-gc documentation to learn more about it.
3. docker-slim: Magic Diet Pill for Your Containers
If you are worried about the size of your Docker images, you will be blown away by docker-slim.
The docker-slim utility uses static and dynamic analysis to create skinny image variants of your fat images. To use docker-slim, you have to download its binary from Github. Binaries are available for Linux and Mac. Once you download the binary, add it to your PATH.
I created a Docker image for example application
friendlyhello used in the Docker official documentation. The image size, as you can see below, is 194 MB.
As you can see for a simple application, we have to download 194 MB of data. Let’s use docker-slim to see how much fat it can remove.
The docker-slim utility will carry out a series of steps–inspecting fat image, instrument fat image, finally creating a slim version of the image. Let’s look at the size of the slim variant.
As you can see above, the image size was reduced to 24.9 MB. You can start the container and it will behave in the same manner. The docker-slim utility works well with Java, Python, Ruby, and Node.js application.
Try it yourself and see how much you can gain. In my personal projects, I have found that it worked for most cases. You can learn more about docker-slim from its documentation.
4. rocker: Breaks the Limits of Dockerfile
Most of the developers using Docker use Dockerfile for building images. Dockerfile is a declarative way to define all the commands a user could call on the command line in order to assemble an image.
Rocker adds new instructions to the Dockerfile instruction set. Rocker was created by Grammarly to solve problems they faced with Dockerfile format. The Grammarly team wrote an in-depth blog explaining their reasons for creating it. I recommend you read it to better understand Rocker. The two problems they highlight in their post are:
- Size of Docker images.
- Slower builds.
The blog also mentions some of the new instructions added by Rocker. Refer to Rocker documentation to learn about all the instructions Rocker supports.
- MOUNT is used to share volumes between builds so they can be reused by dependency management tools.
- FROM instruction exists in the Dockerfile as well. Rocker makes it feasible to add more than one FROM instruction. This means you can create more than one image from a single Rockerfile. The first set of instructions will build the artifact using all the dependencies. The second set of instructions can use the build artifact. This reduces the image size drastically.
- TAG is used to tag the image at different stages of the build. This means you don’t have to tag images manually.
- PUSH is used to push images to a registry.
- ATTACH allows you to run an intermediate step interactively. This is useful for debugging.
To use Rocker, you must install it on your machine. For Mac users, it is as simple as running couple of brew commands:
Once installed, you can use Rocker to build an image by passing it to Rockerfile:
To build an image and push it to Docker Hub, you can run the following command:
Rocker has a good set of features. To learn more about it, refer to its documentation.
5. ctop: Top-like interface for containers
The utility I have started using lately is ctop, which provides a real-time metrics view of multiple containers. If you are a mac user, then you can install ctop using brew as shown below.
Once installed, you can start using ctop. It only needs the DOCKER_HOST environment variable configured.
To view the state of all the containers, you can run
To view the running containers only, you can use
ctop -a command.
The ctop is a simple utility and very useful to learn about containers running on your host. You can read more about it in the ctop documentation.
These are some of the Docker utilities I find useful. Do you use any Docker utilities in your daily work? If so, let us know in the comments section below.
Published at DZone with permission of Shekhar Gulati . See the original article here.
Opinions expressed by DZone contributors are their own.