My Docker Cheat Sheet

How many times have you been working with multiple containers & there is that one *simple* problem that takes you two days to figure out?  You can go get kubernetes & figure all that out or you can do what I do – I keep this Docker cheat sheet opened in notepad so my head doesn’t explode when I need to see something fast & there are seven terminals open.  Wow… This is kind of outdated (05.2019).

##### DOCKER EXEC – RUN COMMANDS WITHIN A CONTAINER
docker exec -it <container_id_or_name> echo “Hello from container!”

##### START A NEW BASH SHELL IN CONTAINER
docker exec -it <container_id_or_name> bash

##### SET AN ENV VARIABLE IN CONTAINER
docker exec -it -e VAR=1 <container_id_or_name> bash

##### LIST ALL EXITED CONTAINERS
docker ps -aq -f status=exited

##### PURGE ALL UNUSED IMAGES, CONTAINER, VOLUMES, & NETWORKS
docker system prune

##### LIST DOCKER IMAGES
docker images -a
docker images -a | grep “pattern”

##### LIST ACTIVE CONTAINERS
docker ps -aq

##### STOP ALL DOCKER CONTAINERS
docker stop $(docker ps -a -q)

##### STOP A SINGLE DOCKER CONTAINER
docker stop <CONTAINER ID>

##### REMOVE ALL CONTAINERS — AFTER STOPPING ALL
docker rm $(docker ps -a -q)

##### REMOVE A SINGLE CONTAINER
docker rm <CONTAINER ID>

##### REMOVE DOCKER IMAGES
docker rmi <IMAGE>
docker images -a | grep “pattern” | awk ‘{print $3}’ | xargs docker rmi

##### LIST DANGLING IMAGES
docker images -f dangling=true

##### REMOVE DANGLING IMAGES
docker images purge

############### DOCKER NETWORKING ##################

##### LIST DOCKER NETWORKS
docker network ls

##### INSPECT DOCKER NETWORKING
docker network inspect -f ‘{{range .Containers}}{{println .Name .IPv4Address}} \{{end}}’ <NETWORK NAME>

##### RETURN INFO ABOUT DOCKER NETWORK
docker network inspect <NETWORK NAME>

################ CLEAN SLATE ##############

##### DELETE EVERY DOCKER CONTAINER
##### Must be run first because images are attached to containers
docker rm -f $(docker ps -a -q)

##### DELETE EVERY DOCKER IMAGE
docker rmi -f $(docker images -q)
docker system prune — FOR GOOD MEASURE

##### DOCKER VOLUMES #####

##### LIST VOLUMES
docker volume ls

##### LIST DANGLING VOLUMES
docker volume ls -f dangling=true

##### REMOVE VOLUMES

docker volume rm volume_name volume_name
docker volume rm `docker volume ls -q -f dangling=true`

#####  NOTES ON DOCKER STATISTICS #########################

curl –unix-socket /var/run/docker.sock http:/containers/json | python -m json.tool

STREAMING STATS: curl –unix-socket /var/run/docker.sock \ http:/containers/8a9973a456b3/stats
ONE SHOT: curl –unix-socket /var/run/docker.sock \ http:/containers/8a9973a456b3/stats?stream=false

cAdvisor is a simple server that taps the Docker API and provides one minute of historical data in 1-second increments. It’s a useful way to visualize what’s going on at a high level with your Docker containers on a given host. cAdvisor simply requires one container per host that you’d like to visualize.

sudo docker run \
 –volume=/:/rootfs:ro \
 –volume=/var/run:/var/run:rw \
 –volume=/sys:/sys:ro \
 –volume=/var/lib/docker/:/var/lib/docker:ro \
 –publish=8080:8080 \
 –detach=true \
 –name=cadvisor \
 google/cadvisor:latest