Skip to content

Instantly share code, notes, and snippets.

@mlapierre
Last active May 9, 2018 13:56
Show Gist options
  • Save mlapierre/cbe4cfcb51c4b065bb96b6e67d08771c to your computer and use it in GitHub Desktop.
Save mlapierre/cbe4cfcb51c4b065bb96b6e67d08771c to your computer and use it in GitHub Desktop.
Docker Cheat Sheet

There are many good cheat sheets out there. This one is a collection of commands and notes I find myself referring to relatively frequently.

Remove stopped containers

docker container prune

Remove all containers that are not running

It will report an error about any running containers, since this attempts to remove them too Note: This will remove ALL containers, including volume data containers. You might not want this

docker ps -qa | xargs docker rm
sudo docker ps -qa | sudo xargs docker rm

Remove unused images

docker image prune --force
docker rmi $(docker images | grep "<none>" | awk "{print \$3}")
docker images | grep "<none>" | awk "{print $3}" | xargs docker rmi

Update all images

docker images | awk '{print $1}' | xargs -L1 docker pull
docker images | awk '(NR>1) && ($2!~/none/) {print $1}' | xargs docker pull
docker pull $(docker images | awk '(NR>1) && ($2!~/none/) {print $1}')

Remove orphaned volumes

docker volume prune --force
docker volume ls -qf dangling=true | xargs docker volume rm

Clone a volume

docker run --rm -v source-volume:/from:ro -v dest-volume:/to debian cp -TR /from /to

Import / export

docker save myusername/myproject:latest | gzip -c > myproject.tar.gz
docker load < image.tar.gz
docker load --input image.tar.gz

Show space usage

docker system df -v

Switch between linux and windows docker daemons

& 'C:\Program Files\Docker\Docker\DockerCli.exe' -SwitchDaemon

Debugging java code using gradle in a container

Start the container with -p 5005:5005 and pass gradle the command --debug-jvm It will wait for the debugger to attach to the socket before running tests

List images in a registry

> curl https://registry.host/v2/_catalog
{"repositories":["repository-name"]}

> curl https://registry.host/v2/oit/docfinity/tags/list
{"name":"repository-name","tags":["1.0.0"]}

Notes

If Hyper-V machines and disks are moved to a different location, make sure the Hyper-V Administrators group has write access to C:\ProgramData\Microsoft\Windows\Hyper-V. It needs it to create symlinks to the new location

Volume mount gotchas

If Docker can't find the location to mount on the host, it mounts the volume as a directory in the container. This can make it seem like a Docker is turning a file into a directory, if the file can't be found. This will happen if you try to use a relative path; the -v command must be given an absolute path (but docker compose can have relative paths) More here: https://stackoverflow.com/questions/34134343/single-file-volume-mounted-as-directory-in-docker

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