DEV Community

Cover image for Useful Docker shell aliases to speed up Development
Kevin Woblick
Kevin Woblick

Posted on

Useful Docker shell aliases to speed up Development

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

Top comments (10)

Collapse
 
danielkun profile image
Daniel Albuschat

Nice cheat-sheet, thanks!

I can add:

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

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.

Collapse
 
abhinav1217 profile image
Abhinav Kulshreshtha • Edited

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.

Collapse
 
m1well profile image
Michael Wellner

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

Collapse
 
rikvanderkemp profile image
Rik van der Kemp • Edited

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

Collapse
 
kovah profile image
Kevin Woblick

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.

Collapse
 
rikvanderkemp profile image
Rik van der Kemp

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

Collapse
 
jwkicklighter profile image
Jordan Kicklighter

Also dce="docker-compose exec -it

Collapse
 
ajboni profile image
ajboni

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?

Collapse
 
frankfont profile image
Frank Font

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

Collapse
 
felipe1982 profile image
Felipe Alvarez

That's for docker compose. Not docker.