Docker Cli Tips and Tricks ========================== - bret@bretfisher.com - https://twitter.com/BretFisher - http://bretfisher.com ## My favorite command line options and best practices - Bash/zsh completion - use *Tab* to populate command line, narrows down options as you go, fills in container names, ID's, images, volumes. Huge time saver. - http://blog.alexellis.io/docker-mac-bash-completion/ - https://github.com/docker/docker/blob/master/contrib/completion/bash/docker - https://docs.docker.com/compose/completion/ - Even without auto complete: you don't need type full container ID's - just type enough of ID to be unique - Example: `docker rmi 7c9 2f 5cad` will remove three images - Name volumes to find/keep easier - Example: `docker run --name psql -d -p 5432:5432 -v psql:/var/lib/postgresql sameersbn/postgresql` - Then: `docker volume ls` - Use ONBUILD Versions for easy development of Node, Ruby, and others - https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#/onbuild - https://hub.docker.com/_/ruby/ - https://hub.docker.com/_/node/ - Always use `docker-compose down` to cleanup properly (networks, containers) - To also remove unnamed containers and volumes: `docker-compose down -v --rmi local` ## My favorite command line aliases (quick container management, usually for local dev) - Only removes stopped containers, doesn't delete data volumes, mostly safe - `alias drma='docker rm $(docker ps -aq)'` - Remove all images, doesn't remove images in use by containers, mostly safe - `alias drmi='docker rmi $(docker images -q -f "dangling=true")'` - Remove all images, doesn't remove images in use by containers, mostly safe - `alias drmai='docker rmi $(docker images -q)'` - For Docker Machine users, set docker cli to use the default VM - `alias drdefault='eval "$(docker-machine env default)"'` - Execute interactive command in running container - `alias drit='docker exec -it'` - Get the IP for eth0 inside the container - `alias drip='docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"'` ### Other lists/dotfiles/functions for less typing - [oh-my-zsh has a add-in](https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/docker/README.md), which I use always - [jessfraz](https://twitter.com/jessfraz) has [some next level shit](https://github.com/jfrazelle/dotfiles/blob/master/.dockerfunc) as bash functions - @tcnksm has [a good list to start with](https://github.com/tcnksm/docker-alias/blob/master/zshrc) ## Cli filters for managing lots of images/containers ### Filters - [Filtering the image list](https://docs.docker.com/engine/reference/commandline/images/#/filtering) - Only list dangling images (built with no name): `docker images --filter "dangling=true"` - [Filtering the containers list](https://docs.docker.com/engine/reference/commandline/ps/#/filtering) - Only list containers that have exited successfully (without error): `docker ps -a --filter 'exited=0'` - Only list containers coming from nginx image: `docker ps --filter ancestor=nginx` ### Formatting - [Change the columns shown in docker command](https://docs.docker.com/engine/reference/commandline/images/#/formatting), great for TPS Reports - `docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}" > images.txt` ## Best list of all things Docker ecosystem - https://github.com/veggiemonk/awesome-docker