Skip to content

Docker CLI Tools Containerization

Docker CLI Tools Containerization refers to the practice of packaging command-line interface utilities inside Docker containers to create isolated, portable, and dependency-free execution environments^[600-developer__docker-run-command.md]. Instead of installing complex software or specific versions of tools directly on the host operating system, users run these tools inside lightweight containers.

This approach allows developers to utilize software without polluting their local environment or managing conflicting dependencies^[600-developer__docker-run-command.md].

Benefits

Containerizing CLI tools provides several advantages for system administration and development workflows:

  • Dependency Isolation: Tool dependencies are confined within the container image, preventing conflicts with host system libraries^[600-developer__docker-run-command.md].
  • Version Management: Multiple versions of a tool can be run simultaneously by switching container images or tags.
  • Host Machine Cleanliness: It avoids the need to manually install or upgrade software on the host machine, keeping the host environment clean.
  • Cross-Platform Execution: A containerized Linux application can often run on Windows or macOS via Docker, bridging OS gaps.

Technical Implementation

The general workflow for creating and using a containerized CLI tool involves building a custom Docker image, aliasing the Docker command, and executing the tool interactively^[600-developer__docker-run-command.md].

1. Create a Dockerfile

The process begins with defining a Dockerfile that specifies the base image and the tool to be included.^[600-developer__docker-run-command.md]

FROM alpine:latest
RUN apk add --no-cache apache2-ssl apache2-utils ca-certificates
ENTRYPOINT [ "ab" ]
^[600-developer__docker-run-command.md]

In this example, the ENTRYPOINT instruction sets the default executable for the container (in this case, ab, the Apache HTTP server benchmarking tool).

2. Build the Image

Once the Dockerfile is created, it is built into a Docker image^[600-developer__docker-run-command.md].

docker build . -t yudady/ab
^[600-developer__docker-run-command.md]

3. Create an Alias

To make the containerized tool feel like a native command, a shell alias is often configured to wrap the docker run command^[600-developer__docker-run-command.md].

alias ab='docker run -it yudady/ab'
^[600-developer__docker-run-command.md]

With this alias, running ab in the terminal automatically executes the containerized version.

4. Execution

Users interact with the tool by passing arguments directly to the alias. Docker passes these arguments to the container's ENTRYPOINT^[600-developer__docker-run-command.md].

$ ab -n 1 -c 1 "http://www.google.com/"
^[600-developer__docker-run-command.md]

Graphical Application Support

While typically used for text-based utilities, this method can also be extended to graphical applications running in a container, such as web browsers (e.g., Tor Browser)^[600-developer__docker-run-command.md]. This requires:

  1. X11 Server: An X server (like VcXsrv on Windows) must be installed and running on the host to handle the graphical display^[600-developer__docker-run-command.md].
  2. Display Configuration: The DISPLAY environment variable must be configured to allow the container to forward graphics to the host screen^[600-developer__docker-run-command.md].

Example command for running a graphical browser^[600-developer__docker-run-command.md]:

docker run -it \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -e DISPLAY=$DISPLAY \
    --name tor-browser \
    jess/tor-browser

Sources

  • 600-developer__docker-run-command.md