Chris Stryczynski

Software Developer / Consultant

Useful docker command line functions

Posted on: 18/07/2017

Having used docker extensively over these last few years, I’ve come up with a few commands / functions that I find really useful, and that save me a whole bunch of keystrokes!

You’ll need FZF installed for some of these commands, which I also highly recommend, if you like FZF you might also like this post Awesome Linux Utilities.

# (requires docker, docker-compose)
docker_stopAllContainers() { sudo docker stop $(sudo docker ps -q); }
docker_deleteComposeVolumes() { sudo docker-compose down -v ; }
docker_showLatestCreatedContainer() { sudo docker ps -a --latest; }
docker_getLatestCreatedContainerId() { sudo docker ps -a --latest -q; }
docker_startLatestCreatedContainer() { sudo docker start "$(sudo docker ps -a --latest -q)" ;}

docker_listContainers() { sudo docker ps -a ; }
docker_listRunningContainers() { sudo docker ps; }
docker_listRunningContainerIps() { sudo docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(sudo docker ps -q) ; }
docker_enterContainerBash() { sudo docker exec -it "$(sudo docker ps --format "{{.Names}}" | fzf | cat)" /bin/bash ; }
docker_enterContainerSh() { sudo docker exec -it "$(sudo docker ps --format "{{.Names}}" | fzf | cat)" /bin/sh ; }
docker_runCommandInContainer() { sudo docker exec -it "$@"; }
docker_runCommandInContainerWithContainerPrompt() { sudo docker exec -it "$(docker_promptForRunningContainer)" "$@"; }
docker_promptForRunningContainer() { echo $(sudo docker ps --format "{{.Names}}" | fzf) ; }

docker_dockerComposeUp() { sudo docker-compose up ; }
docker_dockerComposeStop() { sudo docker-compose stop ; }

All of these commands (and more) are available in the following project:


No comments, yet!

Submit a comment