Docker Tip #26: Alias and Function Shortcuts for Common Commands
Docker related aliases and functions can save you a lot of typing in the long run. Here's how to set up my favorite ones.
Some people like to go all out with creating aliases and functions, such as
mapping things like git clone
to gc
. I’m not one of those people because
I find it harder to remember the shortened version months later because I forgot
what they originally stood for.
But, I do like to sprinkle in a few shortcuts when it makes a lot of sense.
For example, I run the docker-compose exec web
command a lot, followed by
whatever command I want to run inside of my web
service.
I type it often enough that it’s mildly annoying to keep doing it because it’s kind of long. This a good opportunity to create a Bash function to act as a shortcut.
For example, typing dew rails c
is a lot nicer to me than running
docker-compose exec web rails c
. It’s also kind of cool because if you
pronounce “dew” outloud, it sounds like you’re saying “do rails console” which
describes what it’s doing.
To do that, you need to bust out a Bash function. You can do that by opening
your ~/.bashrc
file (or a separate .bash_aliases
file if you have one) and
then add in the function below.
Bash function to shorten a long Docker Compose command:
dew() {
docker-compose exec web $@
}
The $@
part handles passing along all of the function’s arguments. This way
everything passed in after dew
will be sent to docker-compose exec web
.
As pointed out by a reader in the comments, the above would also work as an alias since we’re just passing in a bunch of arguments at the end of the command.
Bash alias to shorten upping your Docker Compose stack:
alias up='docker-compose up'
This one is self explanatory. I type this enough times where I’m willing to shorten it, and there’s no way I’ll ever forget what it does.
Make sure to source ~/.bashrc
for the changes to go into effect after you
save your file.
I’m sure you can do the same with PowerShell if you’re on Windows and can’t use WSL with Docker. I’d really appreciate it if you could comment below with how to do both with PowerShell because I don’t use it personally. Instead, I use WSL and Docker for Windows together because it works flawlessly.
What are some other Docker related aliases and functions you can think of?