Contents

Useful Docker Bash functions and aliases

I type a lot of Docker commands every day. As a result I’ve made a habit of creating Bash functions and aliases that I find useful and adding them to my .bash_profile.1

Getting the IP address

My first useful alias, dip, is a short-cut to the docker inspect command that allows you to inspect a container. One of the command’s features is using the --format flag to select a subset of the inspection data. In this case I’m returning the container’s IP address.

This is my alias:

{% raw %}alias dip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"{% endraw %}

You can then type dip and the container ID or name:

1
2
$ dip 72bbff4d768c
172.17.0.3

This will return the IP address of the specific container.

Removing containers

Once you’re done with a container it is easy to use the docker rm command to remove it. But sometimes you want to delete a lot of containers at once. This function provides a shortcut that removes all non-running containers. It works by passing the output of the docker ps -q -a command, which returns a list of container IDs, to the docker rm command.

To do this removal I wrote this function:

1
drm() { docker rm $(docker ps -q -a); }

I can then run:

1
2
3
4
5
$ drm
b644290130ec
092f4dc1abca
6451afd047d4
Error: Impossible to remove a running container, please stop it first or use -f

You can see that three containers have been removed but one running container has been skipped.

Removing images

Very similar to the function for removing containers is my function for removing images. It passes the output from the docker images -q command to the docker rmi command.

This is the function.

1
dri() { docker rmi $(docker images -q); }

And when run we’ll see:

1
2
3
4
$ dri
Deleted: 78fc424470fd789d8b5d7a0e3c698137000c0819157efbd0f29bcdee0621c567
Deleted: 4040035d043ca0f18807a80873924ea565e3136ccee3e127c22c335ef32b0a9e
Error: Conflict, cannot delete image 1a6d876a1d70 because it is tagged in multiple repositories, use -f to force

We can see it has removed some images but skipped another that is in use.

Running a different types of containers

Next I have two simple aliases that provide shoutcuts to my most common options for running interactive and daemonized containers.

The first alias runs a daemonized container.

1
alias dkd="docker run -d -P"

I use it like so:

1
$ dkd jamtur01/ssh

This will launch a daemonized container running my jamtur01/ssh image. I could also add a command override at the end of the command also.

My second alias is very similar but runs an interactive container instead.

1
alias dki="docker run -t -i -P"

I use this like so:

1
$ dki ubuntu /bin/bash

This will launch a interactive container with a TTY running the ubuntu image and executed with the /bin/bash command.

Docker build function

Finally I have a function for interacting with the docker build command. The function allows me to skip typing the -t flag for tagging my new images.2

1
db() { docker build -t="$1" .; }

I use it like so:

1
$ db jamtur01/ssh

It assumes a Dockerfile in my current directory and then builds that file and tags the subsequent build with jamtur01/ssh.

I hope those are useful to folks and feel free to add others you use in the comments.


  1. These aliases assume you are using Bash but could obviously be easily updated for zsh, fish, etc. ↩︎

  2. Which anyone who has seen me give a Docker demo has seen me typo… :) ↩︎