Skip to content

Instantly share code, notes, and snippets.

@BretFisher
Last active December 6, 2023 19:49
Show Gist options
  • Star 65 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save BretFisher/70c61f0e6099eb60fcc6bc4569f21da9 to your computer and use it in GitHub Desktop.
Save BretFisher/70c61f0e6099eb60fcc6bc4569f21da9 to your computer and use it in GitHub Desktop.
Docker CLI Tips and Tricks

Docker Cli Tips and Tricks

My favorite command line options and best practices

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

Cli filters for managing lots of images/containers

Filters

  • Filtering the image list
    • Only list dangling images (built with no name): docker images --filter "dangling=true"
  • Filtering the containers list
    • 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

Best list of all things Docker ecosystem

@mkrzywanski
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment