Pythian Blog: Technical Track

Simplifying Docker Interactions with BASH Aliases

Landing a Docker Whale
Docker has been consuming my life in the last few weeks. I have half a dozen projects in progress that use containers in some fashion, including my Visualizing MySQL's Performance Schema project. Since I prefer to work from a Mac laptop, I have to utilize a Linux Virtual Machine (VM) which runs the Docker daemon. Luckily, Docker Machine makes this a very simple process. However, interacting both with Docker and Docker Machine does introduce some additional commands that I would rather simplify for the repeatable use-cases I've come across. With BASH aliases, this is not a problem.

Is My Docker Environment Setup?

When working with Docker through Docker Machine, you first have to set up your environment with various DOCKER_* variables, such as these: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-docker-env-out The first alias is an easy way to check that the Docker environment is setup. https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-de_alias-sh Now, all I have to type is de, and I get the Docker environment output: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-de-out

Setting up My Docker Environment

But how do you set up the environment with Docker Machine? The docker-machine command provides the details: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-docker_machine_env-out Notice that the comments indicate you have to run the command through eval to get the terminal setup correctly. I don't want to type that out each time I open a new terminal. The docker-machine command requires the name of the VM to set up as an argument, so I've created a function to accept the argument: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-dm-env_alias-sh Each time I open a terminal I can setup the environment: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-dm-env-out If you only use one Docker VM for local development, you can hardcode the name of it to execute the command to automatically setup the docker environment when a new terminal is created.

Cleaning Out Docker Images

The last helpful alias I have comes from building and re-building containers that have left old images on my VM. https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-dangling_images-out The docker-clean command cleans up all dangling images: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-docker-clean_alias-sh And running the docker-clean command yields: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-docker-clean-out I put all of these aliases and functions together in my ~/.bash_profile* script, which is executed anytime I open a terminal window: https://gist.github.com/dtest/9d23ae5f539f0a37db44#file-aliases-sh *Note: Instead of putting these aliases and functions in ~/.bash_profile, other distributions would look for them in ~/.bashrc or ~/.bash_aliases to ensure they are available for all types of interactive shells. If you have any other commands to simplify Docker interactions, please share them in the comments!   Discover more about our expertise with DevOps.

No Comments Yet

Let us know what you think

Subscribe by email