Creating Docker Aliases

Hello everyone! In one of my previous posts, I mentioned that I want to start using aliases more, and I presented my Git aliases. Today I will expand my aliases collections with Docker aliases.

Hello everyone! In one of my previous posts, I mentioned that I want to start using aliases more, and I presented my Git aliases. Today I will expand my aliases collections with Docker aliases.

As I've suggested in a past post, an alias is a shorthand for a specific command. By utilizing these shorthands, you avoid typing long commands, thus increasing your efficiency and productivity.

List of Docker aliases

As always, I don't want to introduce a vast list of aliases because I tend to forget them. So I favor starting from a small subset that develops with my workflow.

# .docker-aliases.sh
complete -F _docker d

alias d='docker'
alias drit='docker container run -it'

alias db='docker build'
alias dcr='docker container run'
alias dps='docker ps --all'

alias dca='d container ls --all'
alias dia='d images'

alias drmc='docker container rm'
alias drmi='docker image rm'

alias drmca='docker container rm $(docker ps -aq)'
alias drmia='docker rmi $(docker images -q)'

Of course, you also have to load the aliases script from your .bashrc or .bash_profile:

if [ -f ~/.docker_aliases.sh ]; then
. ~/.docker_aliases.sh
fi

Bash Autocomplete

One of the most irritating parts, when you set up aliases, is the autocompletion.
In the beginning, I tried to use a similar approach as with my Git aliases, where I was loading and calling the autocompletion functions directly for each command.

However, for most of the aliases that I listed above, we don't need any bash autocompletion. The only exception is the docker command. On top of that, I found this post that was using the complete command to set the autocomplete function to the alias. Feel free to check it out as it goes a bit deeper into the autocompletion topic.

Summary

So this is it for today! In this short post, we went on to create bash aliases for Docker commands with autocomplete.

Do you use aliases for Docker? If yes, what are some cool aliases you use? Let me know in the comment section below!

I hope you found this post informative! Stay tuned!

Subscribe to Backend Definite

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe