Cheat sheetDev

Docker basics

The Docker commands for images, containers, and Compose you'll reach for constantly — flags explained, not just listed.

Last updated

Images

CommandWhat it does
docker pull nginxDownloads an image from a registry — usually Docker Hub — without running it yet.
docker build -t myapp .Builds an image from the Dockerfile in this folder and tags it myapp.
docker imagesLists every image you've pulled or built locally, with its size.
docker rmi myappDeletes a local image — fails if a container is still using it.
docker tag myapp myapp:v2Gives an image another name — the usual first step before pushing it to a registry.

Containers

CommandWhat it does
docker run -d nginxStarts a container in the background and hands your terminal back immediately.
docker run -p 8080:80 nginxMaps your machine's port 8080 to the container's port 80 — left side is yours, right side is theirs.
docker run -v ./data:/app/data nginxMounts a folder from your machine into the container — files survive even if the container doesn't.
docker psLists running containers — add -a to see stopped ones too.
docker ps -aLists every container, running or not — where 'it disappeared' containers hide.
docker stop mycontainerAsks a container to shut down gracefully, with a grace period before it's forced.
docker rm mycontainerDeletes a stopped container — its filesystem is gone, but named volumes survive.
docker exec -it mycontainer bashOpens an interactive shell inside a running container — for poking around live.
docker logs -f mycontainerStreams a container's output live — the equivalent of tail -f for containers.
docker restart mycontainerStops then starts a container again — for when a service just needs a kick.

Compose

CommandWhat it does
docker compose up -dStarts every service defined in docker-compose.yml, in the background.
docker compose downStops and removes everything compose started — containers, networks, the works.
docker compose logs -fStreams logs from every service in the project, interleaved and labeled.
docker compose psLists just this project's containers, not every container on the machine.
docker compose buildRebuilds images for services that have a build: section, without starting them.

Cleanup & inspect

CommandWhat it does
docker system pruneDeletes stopped containers, unused networks, and dangling images in one pass.
docker system prune -aThe aggressive version — also removes any image not used by a running container.
docker volume lsLists named volumes — where data outlives a docker rm.
docker volume pruneDeletes volumes no container is using — the leftovers system prune skips by default.
docker inspect mycontainerDumps everything Docker knows about a container as JSON — IP, mounts, env vars, all of it.
docker statsA live view of CPU, memory, and network use per container — top, but for containers.
docker cp mycontainer:/app/log.txt .Copies a file out of a container without needing to exec in first.

Worth remembering

  • -p is host:container, always in that order — mixing it up is the single most common docker run mistake.
  • docker rm doesn't touch named volumes — that data survives until you docker volume rm it explicitly.

Related in Cheat sheets