Docker cheatsheet
12 commands, from a Dockerfile to a running container and a registry. An image is the packed app; a container is one running copy of it. The sheet plays them in order; pick one to watch it again.
docker build -t shop:1 .Read the Dockerfile here and build an image called shop:1Build images
Run containers
Share and clean up
Build images
docker build -t shop:1 .Read the Dockerfile here and build an image called shop:1docker imagesList the images on this machine. Changes nothingRun containers
docker run -d --name web shop:1Make a container from the image and run it in the background (-d)docker psList running containers (-a adds the stopped ones)docker logs webSee what the container printed (-f keeps following)docker exec -it web shOpen a shell inside the running containerdocker stop webStop the container (SIGTERM, a wait, then SIGKILL)docker rm webRemove the stopped container; the image staysShare and clean up
docker tag shop:1 registry.example/shop:1Give the same image another name, which says where it will be pusheddocker push registry.example/shop:1Send the image to a registry, for other machines to pull (checked as a command only: it needs a registry)docker pull registry.example/shop:1Fetch an image from a registry onto this machine (checked as a command only: it needs a registry)docker rmi registry.example/shop:1 shop:1Remove images (no container may be using them)Image or container?
An image never changes: build it once, run it many times. A container is a running copy with its own writable layer, so what you change inside one is gone when you rm it. Keep data in a volume.
Checked
docker.sh
#!/bin/sh
# Every command on the Docker cheatsheet, run for real against a small image built from alpine:3.20 (already here: nothing
# is pulled). After each one, a line saying what Docker holds: images, containers and their state. The page's pictures
# are held to these lines (lib/cheat-docker.test.ts). push and pull need a registry, so they are checked as commands
# only (docker push --help). Everything made here has a mindloop-cs- name and is removed at the end.
# Run: sh docker.sh (needs docker)
set -eu
P=mindloop-cs-
work=$(mktemp -d)
tidy() { docker rm -f "${P}web" >/dev/null 2>&1 || true; docker rmi -f "${P}shop:1" "registry.example/${P}shop:1" >/dev/null 2>&1 || true; }
trap 'tidy; rm -rf "$work"' EXIT
tidy
cat > "$work/Dockerfile" <<'DOCKERFILE'
FROM alpine:3.20
CMD ["sh", "-c", "echo shop started; echo listening on 8080; sleep 600"]
DOCKERFILE
say() { # what Docker holds now, in the words of lib/cheat-docker.ts summary()
images=$(docker images --format '{{.Repository}}:{{.Tag}}' | sed -n "s/$P//p" | sort | tr '\n' ' ' | sed 's/ $//')
ctrs=$(docker ps -a --filter "name=^${P}" --format '{{.Names}}:{{.State}}' | sed "s/$P//" | tr '\n' ' ' | sed 's/ $//')
echo "$1: images=[$images] containers=[$ctrs]"
}
docker build -q -t "${P}shop:1" "$work" >/dev/null; say build
docker images >/dev/null; say images
docker run -d --name "${P}web" "${P}shop:1" >/dev/null; sleep 1; say run
docker ps >/dev/null; say ps
test "$(docker logs "${P}web")" = "shop started
listening on 8080"; say logs
test "$(docker exec "${P}web" sh -c 'echo inside; hostname' | head -1)" = inside; say exec
docker stop -t 1 "${P}web" >/dev/null; say stop
docker rm "${P}web" >/dev/null; say rm
docker tag "${P}shop:1" "registry.example/${P}shop:1"; say tag
docker push --help >/dev/null; docker pull --help >/dev/null; echo "push, pull: checked as commands only (no registry here)"
docker rmi "registry.example/${P}shop:1" "${P}shop:1" >/dev/null; say rmi
Run:
sh docker.sh (needs Docker)It printed:build: images=[shop:1] containers=[] images: images=[shop:1] containers=[] run: images=[shop:1] containers=[web:running] ps: images=[shop:1] containers=[web:running] logs: images=[shop:1] containers=[web:running] exec: images=[shop:1] containers=[web:running] stop: images=[shop:1] containers=[web:exited] rm: images=[shop:1] containers=[] tag: images=[registry.example/shop:1 shop:1] containers=[] push, pull: checked as commands only (no registry here) rmi: images=[] containers=[]
Every command but push and pull run against Docker on a tiny image built from alpine:3.20 (nothing pulled), each line what Docker held after. push and pull need a registry, so they are checked as commands only. All of it removed at the end.