Popular Articles

The XebiaLabs DevOps Platform provides visibility and synchronizes data across the CI/CD pipeline by connecting issue tracking and ITSM ticketing tools, so that user stories and change tickets are always up to date.

Subscribe for more!

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.

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 support 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.

WHITE PAPER

How can enterprises take advantage of the benefits of container technology without creating more work for their teams?

Check out this free whitepaper to learn where containers fall short and how Release Orchestration and Deployment Automation tools can bridge the gap between the promise of containers and the realities of complex enterprise application delivery.

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.

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.

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 good set of features. To learn more about it, refer to its documentation.

Shekhar Gulati is a polyglot programmer and technology evangelist for XebiaLabs.

The Reality of Software Releases

Many organizations model software delivery as if the features that are initially planned for a release are always the features that are actually delivered to production when the release is done. But the reality of software releases is more complicated than that, because it’s hard to predict the delivery of planned features. The XebiaLabs DevOps Platform can help you deal with the reality of software releases. See for yourself!

Start Your Trial

The XebiaLabs DevOps Platform provides the intelligence, automation, and control that technical and business teams need.