Simplifying Docker Interactions with BASH Aliases

Posted in: DevOps, Technical Track
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:

The first alias is an easy way to check that the Docker environment is setup.

Now, all I have to type is de, and I get the Docker environment output:

Setting up My Docker Environment

But how do you set up the environment with Docker Machine? The docker-machine command provides the details:

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:

Each time I open a terminal I can setup the environment:

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.

The docker-clean command cleans up all dangling images:

And running the docker-clean command yields:

I put all of these aliases and functions together in my ~/.bash_profile* script, which is executed anytime I open a terminal window:

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



Want to talk with an expert? Schedule a call with our team to get the conversation started.

About the Author

Derek Downey is the Director of Managed Services for the OpenSource Database practice at Pythian, helping to align technical and business objectives for the company and for our clients. Derek loves automating MySQL, implementing visualization strategies and creating repeatable training environments.

2 Comments. Leave new

Glenn Jackman
October 9, 2015 10:09 am

The `dm-env` function can be modified to use a default value:

function dm-env() {
eval “$(docker-machine env “${1:-default}”)”

If you do not provide an argument to the function, the string “default” will be used instead. Also, adding quotes as per shell scripting best practices.


Thanks Glenn, updated!


Leave a Reply

Your email address will not be published. Required fields are marked *