🖥️docker
➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the docker command with important options and switches using examples.
6 minute read
▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁
# ██████╗ ██████╗ ██████╗██╗ ██╗███████╗██████╗
# ██╔══██╗██╔═══██╗██╔════╝██║ ██╔╝██╔════╝██╔══██╗
# ██║ ██║██║ ██║██║ █████╔╝ █████╗ ██████╔╝
# ██║ ██║██║ ██║██║ ██╔═██╗ ██╔══╝ ██╔══██╗
# ██████╔╝╚██████╔╝╚██████╗██║ ██╗███████╗██║ ██║
# ╚═════╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
# Start docker daemon
docker -d
# start a container with an interactive shell
docker run -ti <image_name> /bin/bash
# "shell" into a running container (docker-1.3+)
docker exec -ti <container_name> bash
# inspect a running container
docker inspect <container_name> (or <container_id>)
# Get the process ID for a container
# Source: https://github.com/jpetazzo/nsenter
docker inspect --format {{.State.Pid}} <container_name_or_ID>
# List the current mounted volumes for a container (and pretty print)
# Source: http://nathanleclaire.com/blog/2014/07/12/10-docker-tips-and-tricks-that-will-make-you-sing-a-whale-song-of-joy/
docker inspect --format='{{json .Volumes}}' <container_id> | python -mjson.tool
# Copy files/folders between a container and your host
docker cp foo.txt mycontainer:/foo.txt
# list currently running containers
docker ps
# list all containers
docker ps -a
# list all images
docker images
# Remove all intermediate docker images after build
docker image rm $(docker image list -f "dangling=true" -qa)
# Remove all intermediate docker images after build
docker images | grep <none> | awk '{ print $3; }' | xargs docker rmi
# Docker: Remove all exited docker container
docker ps -aq --filter status=exited | xargs docker rm
# Docker: Remove all exited docker container -> This will do a clean up of stopped containers and volumes used by them Show Sample Output
docker system prune --volumes -f
docker images | grep -v REPOSITORY | awk '{print $1}' | xargs -L1 docker pull
# Update all Docker Images
# Docker: Remove all exited docker container
docker ps -aq --filter status=exited | xargs docker rm
# Remove all intermediate docker images after build
docker images | grep <none> | awk '{ print $3; }' | xargs docker rmi
# Docker: Remove all exited docker container - This will do a clean up of stopped containers and volumes used by them Show Sample Output
docker system prune --volumes -f
# Remove all intermediate docker images after build
docker image rm $(docker image list -f "dangling=true" -qa)
# kill the first docker container in docker ps -> Useful when developing and you donut usually have other docker commands running Show Sample Output
docker kill $(docker ps -q)
# Remove all docker images to cleanup disk -> Remove unused images -a Remove all unused images, not just dangling ones -f Do not prompt for confirmation Show Sample Output
# WARNING! This will remove all dangling images.
# Are you sure you want to continue? [y/N]
# Total reclaimed space: 0B
docker rmi `docker images -a -q`
# Stop all running docker containers - passin list of docker container IDs to docker stop
docker stop $(docker ps -a -q)
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock assaflavie/runlike [ID_CONTAINER]
# Get a docker container's run command line
# A good way to build a new container when you don't remember how you did it the first time This is sample output
docker run --name=mariadb --hostname=43bdf5467ad --env="MYSQL_ROOT_PASSWORD=xxxxxxxx" --env="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" --env="GOSU_VERSION=1.10" --env="MARIADB_MAJOR=10.1" --env="MARIADB_VERSION=1:10.1.36+maria-1~bionic" --volume="/var/lib/mysql" -p 3308:3306 --restart=always --detach=true mariadb:10.1 mysqld
docker ps -a --format '{{ .ID }}' | xargs -I {} docker inspect -f '{{ .Name }}{{ printf "\n" }}{{ range .Mounts }}{{ printf "\n\t" }}{{ .Type }} {{ if eq .Type "bind" }}{{ .Source }}{{ end }}{{ .Name }} => {{ .Destination }}{{ end }}{{ printf "\n" }}' {}
# List docker volumes by container
mkdir -p src && chmod 777 src && docker run -v $(pwd)/src:/var/www/html wordpress:cli core download && chmod 755 src
# Download WordPress using Docker - This is sample output - yours may be different.
Downloading WordPress 4.9.8 (en_US)...
md5 hash verified: c4e7d68b5c382fbbaf547f2b2d42c198
Success: WordPress downloaded.
# Remove all container from an specific network (docker)
docker ps -a -f network=$NETWORK --format='{{.ID}}' | xargs docker rm -f
# Remove all container from an specific network (docker)
docker ps -a -f network=$NETWORK --format='{{.ID}}' | xargs docker rm -f
# Explanation: docker ps -a -f network=$NETWORK --format='{{.ID}}' returns the id's of all container that are subscribed to the network and pass the output to xargs docker rm -f that stop and deletes each container
# Delete all untagged Docker images
docker images -q -f dangling=true | xargs --no-run-if-empty --delim='\n' docker rmi
# Explanation: It does not return a failing exit code if there are no images removed. It should always succeed unless there was an actual problem removing a Docker image.
# Limitations: This only works in the GNU version of xargs (thanks to the --no-run-if-empty), BSD does not have an equivalent that I know about.
## Related one-liners
# Delete all untagged Docker images
docker rmi $(docker images -f "dangling=true" -q)
# Explanation: docker images outputs all images currently available. By specifying -f "dangling=true" we restrict the list to "dangling" images (i.e. untagged). By specifying the -q option we use quiet mode, which limits the output to the images hash, which is the directly fed into docker rmi, which removes the images with the corresponding hashes.
# Delete all untagged Docker images
docker rmi $(docker images -f "dangling=true" -q)
# Explanation: docker images outputs all images currently available. By specifying -f "dangling=true" we restrict the list to "dangling" images (i.e. untagged). By specifying the -q option we use quiet mode, which limits the output to the images hash, which is the directly fed into docker rmi, which removes the images with the corresponding hashes.
## Alternative one-liners:
# Delete all untagged Docker images
docker images -q -f dangling=true | xargs --no-run-if-empty --delim='\n' docker rmi
# Explanation: It does not return a failing exit code if there are no images removed. It should always succeed unless there was an actual problem removing a Docker image.
# Limitations: This only works in the GNU version of xargs (thanks to the --no-run-if-empty), BSD does not have an equivalent that I know about.
# Remove docker images older than one month
docker image ls --format '{{ json . }}' | jq -r -s '.[] | select(now - (.CreatedAt | strptime("%Y-%m-%d %H:%M:%S %Z") | mktime) > 3600 * 24 * 30) | .ID' | xargs -r docker image rm
# List docker log sizes and remind how to empty them
docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs sudo du -hl; sleep 1; printf "\r\n echo '' > \$(docker inspect --format={{.LogPath}} container_name_or_id) \r\n*****COPY****ABOVE****TO******CLEAR*****LOG*****CHANGE***CONTAINER_ID******* \r\n \n"
#==============================##==============================#
# CMD docker #
#==============================##==============================#
Cheatsheets are an excellent complement to other information sources like Linux man-pages, Linux help, or How-To’s and tutorials, as they provide compact and easily accessible information. While man-pages and detailed tutorials often contain comprehensive explanations and extensive guides, cheatsheets summarize the most important options forthe command docker in a clear format. This allows users to quickly access the needed information for docker without having to sift through lengthy texts. Especially in stressful situations or for recurring tasks, cheatsheets for docker are a valuable resource to work efficiently and purposefully.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
█║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌
██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗███████╗██████╗
████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
╚██╔═██╔╝██║ ██║██║ ██║ ╚███╔╝ █████╗ ██║ ██║
████████╗██║ ██║██║ ██║ ██╔██╗ ██╔══╝ ██║ ██║
╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═════╝
█║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.