Docker
Containers, images, logs, Docker Compose and disk cleanup.
Containers
Running containers
docker psAll containers, including stopped
docker ps -aRun a container in the background with a published port
docker run -d --name <container> -p 8080:80 <image>
Open a shell inside a container
docker exec -it <container> sh
Stop and remove a container
docker rm -f <container>
Remove exited containers
docker rm $(docker ps -a -q -f status=exited)Remove created containers
docker rm $(docker ps -a -q -f status=created)Follow logs
docker logs -f --tail 100 <container>
CPU and memory usage
docker statsContainer IP addressadvanced
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container>
\{{end}} is an escaped Go template — it is copied as {{end}}.
Images
List images
docker imagesBuild an image
docker build -t <image> .
Build for another platformadvanced
docker buildx build --platform linux/amd64 -t <image> --push .
Copy a file out of a container
docker cp <container>:/path/in/container ./local-path
Docker Compose
Start everything in the background
docker compose up -dRebuild and start
docker compose up -d --buildStop and remove containers
docker compose downStop and remove including volumes
docker compose down -vService logs
docker compose logs -f <compose_service>
Run a command in a service
docker compose exec <compose_service> sh
Cleanup
How much space Docker uses
docker system dfRemove everything unusedadvanced
docker system prune -a --volumesRemoves all stopped containers, unused images, networks and volumes. Data in volumes will be lost!
Networks and volumes
List networks
docker network lsCreate a network so containers reach each other by name
docker network create <docker_network>
Run a container in that network
docker run -d --name <container> --network <docker_network> <image>
Containers and IPs in a networkadvanced
docker network inspect <docker_network>
List volumes
docker volume lsWhere a volume lives on disk
docker volume inspect <docker_volume>
Back up a volume to a tar archiveadvanced
docker run --rm -v <docker_volume>:/data -v "$PWD":/backup alpine tar czf /backup/<docker_volume>.tar.gz -C /data .
Remove unused volumesadvanced
docker volume pruneData in removed volumes is gone for good.
Inspecting and contexts
Why a container stopped (exit code, OOM)
docker inspect <container> --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}}'
Image layers and their sizes
docker history <image>
Real-time Docker eventsadvanced
docker eventsRun docker commands against a remote host over SSHadvanced
docker context create <docker_context> --docker "host=ssh://<user>@<host>" && docker context use <docker_context>