Motivation For Sharing

Believe it or not, I wasn’t always capable of delivering complete software. Early on in my career configuring and serving applications was the most stressful thing I could imagine. Between my early startup days and working in two 15-20 person developer teams I’ve spent hundreds of hours attempting to get code running on other computers than my own and vice versa . As a developer, there is little more enfuriating than the dreaded phrase “Well it’s working for me”. It doesn’t matter what side of that statement you are on; no one is happy. Docker is a great boon for solving this exact problem.

I was an early adopter of docker in 2015 thanks to my colleague Joshua Cox at WebHosting.coop. I’ve been using Docker since. Docker is all the rage in 2020, so i’d like to share my most used commands from the last 5 years of Docker usage.

Docker: A Powerful, Yet Verbose Api

After getting started with Docker, and getting over the initial learning cuve it should be evident there is a lot of repetitive commands. This is the entire purpose behind Docker Compsose, and commands like docker system prune. These are all fantastic additions to the command line api, but there are still precious keystrokes to save!

Swiss Army Chainsaw

Command-line Docker Shortcuts

Add the following to your shell profile. I use Z Shell, so you can find all of these listed in my .zshrc file

General Shortcuts

  • Saving precious keystrokes

     alias comp='docker-compose'
    
  • alias for different docker-compose.yml file.

     alias comp_prod='docker-compose -f docker-compose.prod.yml'
    
  • Open your docker host in Chrome at a given port (OSX only - you can replace this path with your own Chrome path).

     dopen(){ /usr/bin/open -a "/Applications/Google Chrome.app" "//$DOCKER_HOST:$1";} 
    
  • d_build will let you pass an image name and build the current directory.

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

Containers

  • toss allows you to spin up an ephermeral and itneractive container.

     toss(){docker run -it --rm $@}
    

    Not only useful when just messing around, I use this whenever I am debugging an image build. When you see…

     Removing intermediate container 9ad8456f80d0
      ---> 201553dcbaa8`
    

    The ---> 201553dcbaa8 is a container id!! You can easily jump into this build state by running toss 201553dcbaa8 bash

  • snatch allows you to run commands against an already initialized container.

     snatch(){docker exec -it $@}
    
  • hijack is the same thing as snatch, but just drops into a bash shell. I found mysely appending bash MOST of the time i want to execute against a container so I opted in to add this as well.

     hijack(){docker exec -it $1 bash}
    
  • gime is a quick way to chown files generated in a docker container and linked in a volume.

     gime() { sudo chown -R $USER:$USER $@;}
    
  • stop_contianers is pretty self explanitory. Gracefully stops all of your running containers.

     alias stop_containers='docker stop $(docker ps -q)'
    
  • kill_containers for the impatient.

     alias kill_em_all='docker kill $(docker ps -q)'
    

Houskeeping

Some of these are superceeded by the docker system prune command, however, sometimes thats a little to heavy-handed. Having some more fine-grained controls is useful.

  • remove_containers will delete any non-running container

     alias remove_containers='docker rm `docker ps -aq`'
    
  • nuke_containers remove all containers.

     alias nuke_containers='docker rm -f $(docker ps -a -q)'
    
  • tactical_nuke will remove any non-running container and their images. This is very close to docker system prune, leaving all volumes, networks and cache.

     alias tactical_nuke='remove_containers && remove_images'
    
  • remove_images - remove images that have no running container

     alias remove_images='docker rmi `docker images -q`' 
    
  • nuke_volume - remove any volumes not associated with a running container.

     alias nuke_volumes='docker volume rm $(docker volume ls -q)'
    
  • nuke_from_orbit less interesting considering docker system prune --volumes -a -f is now in the client, but occasionally I would like to blow away everything but my docker networks.

     alias nuke_from_orbit='nuke_containers && docker rmi -f `docker images -q` && docker volume rm `docker volume ls -q`'
    

Bonus: alias common framework specific things

  • bootstrap_django creates a throwaway container and generates a new django project that your user will own by default

     function bootstrap_django(){docker run -it --rm --user "$(id -u):$(id -g)" -v "$PWD":/usr/src/app -w /usr/src/app django django-admin.py startproject $1}
    
  • manage_c lets me execute django management commands](https://docs.djangoproject.com/en/dev/ref/django-admin/)

     manage_c(){snatch $1 python manage.py ${@:2}}
    

The Takeaway

The Docker API is complicated for a reason, but most commands you will execute are similar enough to abstract into bash aliases. I’ve always believed that the best programmers are the right kind of lazy. Some of these shortcuts are most definitely contrived and may only save several keystrokes; but I’ve found these commands greatly reduce the friction I experience when issuing common docker commands.

What bash aliases do you use? Am I miskten? For now, you can submit a PR. WAIT until I get my contact form up. I’ll aslo be extracting my Unify jekyll theme into something private so this repo can be public again.

Next Week

Look forward to me building a simple contact form for this website! I will be building a Flask API that this static website of mine communicates with and sends emails with mailgun.