Weekly Journal 3 - Tool Containers

Introduction

This week I have something a little different. At work we have been using containers for running our automation tools. Containers make it easy to share our tooling in a consistent manner, without having to worry about the classic, “It works on my machine!” issues. I have found that containers also make it easy to have multiple versions of a tool available. Containers are also an excellent way to run Linux tools on Windows. It has worked so well, I have been experimenting with putting tools into containers for some of my personal projects. I figure it might be fun to share one of them here.

HTTPie

HTTPie is a CLI HTTP client, written in Python. It is similar to curl, but in my opinion has a much nicer interface. As it is a Python program, putting it into a container allows me to choose the version of Python specifically for this tool. For example, if I needed to run a tool that only works with Python 2, but the system Python on my machine is Python 3, I can create a container based on Python 2 without disrupting the system Python installation.

Dockerfile

I used Docker Desktop for Windows to create the HTTPie container, but this should work equally well with Docker for Mac or Docker Community Edition on Linux. To create the container first you need to create a Dockerfile that defines the contents of the container. For HTTPie, I used this Dockerfile from GitHub:

1
2
3
4
5
6
7
8
FROM alpine:latest

RUN apk add --no-cache python3 && \
pip3 install --upgrade pip setuptools httpie && \
rm -r /root/.cache

ENTRYPOINT [ "http" ]
CMD ["--help"]

Building the Container

To build the container, run the following command from the directory that contains the Dockerfile:

>docker build -t alpine-docker/httpie .

After a minute or so, the container should be built and in the Docker image cache.

Setting Up the Alias

To run the container, type the following command:

>docker run -t -i alpine-docker/httpie

If everything has worked properly, you should see the HTTPie help text. Typing in that command every time you want to use a tool is awkward at best, so we will set up an alias to make it easier.

For Windows Powershell, add these two lines to your profile:

1
2
function httpie_container() {docker run -i -t alpine-docker/httpie @Args}
Set-Alias httpie httpie_container

For Linux, add this line to your shell profile:

1
alias httpie="docker run -ti alpine-docker/httpie"

With the alias in place, you can type httpie to run the container and HTTPie:

>httpie -v https://duckduckgo.com

Wrapping Up

For the most part, setting up individual tools in containers is a straightforward process. The primary challenge is building the Dockerfile. In many cases, at least for popular utilities, you can likely find an existing Dockerfile on GitHub or Docker Hub, and either copy it wholesale or use it as an example to get started. The next step for me is to build more of these containers so I can have an entire library of tools available.