Put your tools in containers
Juggling a huge number of tools in DevOps can easily become overwhelming. Let's see how Docker, together with lightweight containers, can make your workflow simpler and smoother.
Introduction
Juggling a huge number of tools in DevOps can easily become overwhelming. To make things simpler, let’s look at how Docker, together with lightweight containers, can make your workflow smoother. Picture it as building a versatile toolbox shaped around exactly what you need.
A generic, extendable Docker image for DevOps tasks
DevOps work frequently calls for a variety of tools. Installing all of them on your local machine isn’t always an option. In such situations, Docker images containing every required tool come to the rescue. Such an image can also serve as the base for other images whenever more tools need to be added later on. Alpine Linux is the image of choice for many DevOps engineers, as it is small and fast. The following command runs alpine linux as a docker container:
docker run -it --rm -v ${HOME}:/root/ -v ${PWD}:/work -w /work --net host alpine sh
Once the command runs, we’re logged into the container as the root user. For readers new to Docker, here is what each part of the command does:
-itstarts the container in interactive mode and attaches a pseudo-TTY, meaning we can work with the terminal inside the container--rmdeletes the container once it exits, since we don’t want it left hanging around after we leave the container-vmounts a local directory into the container, giving the container access to local files. Replace the local directory with whichever one you want mounted in the container-wmakes the host working directory the container’s working directory, sparing us from changing directories with thecdcommand--net hosttells docker to use the host’s network stack within the container.
A container built from the Alpine Linux image (default latest tag) ships only with basic tools such as ls, cd, cp, mv and so on. Any other tool has to be installed with the apk package manager. Alpine Linux offers a wide range of packages. The list of Alpine Linux packages is available here, or you can look up the package you need with this command:
apk search --no-cache -f <package_name>
For instance, searching for curl is done with the following command:
apk add --no-cache search -f curl
[...]
curl-8.5.0-r0
[...}
The curl package is then installed with:
apk add --no-cache curl
The --no-cache option stops apk from caching the index locally, which is the recommended practice for Docker images
Once we leave the container with the exit command, the following line saves the container as an image:
docker commit <container_id> <image_name>
From now on, the newly created image lets us start a container that already has all the tools we need. Repeat this approach for several images and you’ll build up a library of images, each with its own set of installed tools. The docker imagescommand shows the list of images. My own list includes these tools: kubeclt, helm, awscli, kubectl, velero, flux, istio. It is certainly going to get longer, though.
Bear in mind that Alpine Linux as a base image has its drawbacks too, for example the lack of the GNU C Library (glibc). Because of this, some applications, such as the Chef client, are not available in the Alpine Linux repository. If you need software that relies on glibc, like the Chef client, one possibility is to pick a base image that ships with glibc. A number of Linux distributions, among them Ubuntu, Debian and CentOS, provide such ima
A toolkit for working on a Kubernetes cluster
The Docker Images built in the previous section work in a local environment. Quite often, however, the tools are needed on a Kubernetes cluster. Say we need a mysql client to connect to a database that runs on the cluster, or one managed by a cloud provider and reachable from the cluster. Then the images have to be pushed to a container registry that the cluster can access. I personally use AWS ECR. Before pushing the image to ECR, it first has to be tagged with the ECR repository URI.
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <ecr_repository_uri>
docker tag <image_name> <ecr_repository_uri>
docker push <ecr_repository_uri>
AWS ECR isn’t the only choice. Each cloud provider offers a container registry of its own (Google Cloud Registry, Azure Container Registry, etc.). There are also public container registries such as Docker Hub, Quay, etc. These work with any Kubernetes cluster.
Building images declaratively
If we’d rather not build and host images ourselves, Nixery is an option. Nixery is a container registry that builds images with the Nix package manager. Thanks to its declarative approach, we choose which applications go into the image simply by listing the tools we need in the image name. Suppose we need helm, kubectl, jq, yq, git, etc. Then we can use this image name: nixery.dev/shell/helm/kubectl/jq/yq/git. It is used just like any other image:
docker run -it --rm -v ${HOME}:/root/ -v ${PWD}:/work -w /work --net host nixery.dev/arm64/bash/helm/kubectl/jq/yq/git bash
Downloading the image can take some time, or at least it did for me. The image is also fairly large (427MB). Still, how easy it is to use, and not having to build and host our own images, are major advantages.
Real world example
chart-testing
In my most recent project, I had to test helm charts with the chart-testing tool. Rather than installing it on my local machine, I made a docker image with the chart-testing tool inside and ran the helm chart tests with it. You can find the image here. This is the command I ran the tests with:
$ docker run -it --rm -v ${HOME}:/root/ -v ${PWD}:/work -w /work --net host quay.io/helmpack/chart-testing ct lint --charts=charts/my-helm-chart-source --validate-maintainers=false
Linting charts...
Version increment checking disabled.
------------------------------------------------------------------------------------------------------------------------
Charts to be processed:
------------------------------------------------------------------------------------------------------------------------
my-helm-chart-source => (version: "0.4.8", path: "charts/my-helm-chart-source")
------------------------------------------------------------------------------------------------------------------------
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "my-helm-chart-source" chart repository
Update Complete. ⎈Happy Helming!⎈
Saving 1 charts
Downloading intouch-libchart from repo https://nexus.td-dev-first.com/repository/intouch-helm/
Deleting outdated charts
Linting chart "my-helm-chart-source => (version: \"0.4.8\", path: \"charts/my-helm-chart-source\")"
Validating /work/charts/my-helm-chart-source/Chart.yaml...
Validation success! 👍
==> Linting charts/my-helm-chart-source
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
------------------------------------------------------------------------------------------------------------------------
✔︎ my-helm-chart-source => (version: "0.4.8", path: "charts/my-helm-chart-source")
------------------------------------------------------------------------------------------------------------------------
All charts linted successfully
You may have noticed that I didn’t run a shell inside the container. Instead, the command launched the chart-testing tool directly. Since the local directory was mounted as a volume, the container could read the helm chart files. When necessary, I can also write the command’s output to the local directory, so there’s no need to copy files out of the container.
A specific application version distributed as a binary
Sometimes a tool is needed in one particular version. In my case, I had to use istioctl in version 1.16.6, while the newest release was 1.19.3 . Since I didn’t want it installed on my local machine, I built a docker image with the istioctl tool in it. The binary is available on the github releases page. These are the commands I used to run the tool:
docker run -it --rm -v ${HOME}:/root/ -v ${PWD}:/work -w /work --net host alpine sh
apk install --no-cache curl
curl -o /tmp/istioctl-1.16.6-linux-amd64.tar.gz https://github.com/istio/istio/releases/download/1.16.6/istioctl-1.16.6-linux-amd64.tar.gz
tar -C /tmp/ -zxvf /tmp/istioctl-1.16.6-linux-amd64.tar.gz
mv /tmp/istioctl /usr/local/bin/istioctl
chmod +x /usr/local/bin/istioctl
With those commands executed, I could generate the manifest for the istio installation:
istioctl manifest generate -f istio-operator.yaml > istio-1.19.3.yaml
Because I only needed the tool once, albeit for several istio versions, I didn’t bother saving the image. I simply ran the commands above to install the tool and produce the manifest.
Conclusion
All in all, Docker is a powerful way to manage DevOps tools, particularly in combination with Alpine Linux. Ready-made images let us keep every tool we need in a single place and reach for it whenever necessary, without installing anything on the local machine. Not only does this make your workflow simpler, it also keeps you efficient whether you work locally, on Kubernetes clusters or in CI/CD pipelines. Try it out and see how your toolkit changes!