loading...
Cover image for Useful Docker shell aliases to speed up Development

Useful Docker shell aliases to speed up Development

kovah profile image Kevin Woblick ・2 min read

I work with Docker all the time. Ditching my MAMP stack for Docker was one of the most convenient and useful decisions I've ever made. If you are interested in Docker you should take a look at my current
Docker Stack which describes how I use Docker for
PHP projects.

Useful Shell aliases

If you work with Docker you have to issue a lot of shell commands to start, stop and manage the containers. Most of the commands are usually targeted towards Docker Compose which is a managing layer for Docker. To improve working with the shell I created several aliases which shorten the amount of typing needed to perform the most common actions.

Container management

# Start the docker-compose stack in the current directory
alias dcu="docker-compose up -d"

# Start the docker-compose stack in the current directory and rebuild the images
alias dcub="docker-compose up -d --build"

# Stop, delete (down) or restart the docker-compose stack in the current directory
alias dcs="docker-compose stop"
alias dcd="docker-compose down"
alias dcr="docker-compose restart"

# Show the logs for the docker-compose stack in the current directory
# May be extended with the service name to get service-specific logs, like
# 'dcl php' to get the logs of the php container
alias dcl="docker-compose logs"

# Quickly run the docker exec command like this: 'dex container-name bash'
alias dex="docker exec -it"

System-wide management

# 'docker ps' displays the currently running containers
alias dps="docker ps"

# This command is a neat shell pipeline to stop all running containers no matter
# where you are and without knowing any container names
alias dsa="docker ps -q | awk '{print $1}' | xargs -o docker stop"

Those are the current aliases I use to speed up development. Feel free to extend them, please share them with me via Twitter.


This article was first posted on Blog.Kovah.de

Posted on by:

kovah profile

Kevin Woblick

@kovah

Full Stack Dev & DevOps Manager @ Taikonauten | Creator of Game-Quotes.com, LinkAce and InvoicePlane. Sponsor me on Github: github.com/sponsors/kovah

Discussion

markdown guide
 

Nice cheat-sheet, thanks!

I can add:

# Remove stopped containers, unused images, unused networks, etc.:
alias dsp="docker system prune"

I'd recommend starting all alias names with a common prefix, so that you can list the available aliases with tab-completion. Something uncommon like dk or something. Then you can do dk<TAB> and see all your aliases.

 

Good to know I am not the only one with this idea. I prefix all my tool related alias like fld_prune = docker system prune and flk_delete = kubectl delete. fl is my handler, fld_ are my docker alias, flk_ are kube related aliases. fly_ are youtube-dl related alias. Only tools related aliases are prefixed, common aliases are not prefixed so cls=clear and md=mkdir -p are unprefixed.

 

I also have a cheatsheet and a respondiere tool with wich i could take a look at the cheatsheet via shell and execute commands instantly.
Also including some predefined databases.
Cheatsheet: github.com/m1well/env-setup/blob/m...
Tool: github.com/m1well/cheatsheet

 
 

Cool! You can simplify your docker stop command like this (if you want)

docker stop $(docker ps -q)

edit: remove 'a' from the ps command

 

You can speed up this command by simply removing the a flag. It gets all Docker containers, even those which are stopped, which makes no sense for stopping all containers.

 

You are right, also using this to remove all so got confused 😜

 

I've been using something similar for kubernetes and it's a life changer. (github.com/ahmetb/kubectl-aliases)
maybe we should make a docker repo?

 

My work buddies and I took the alias idea to next level and created github.com/bah-insignia/zcmd

 

That's for docker compose. Not docker.