Docker Commands

Docker commands cheatsheet.

Build images, run containers, and use Compose — the Docker commands you actually need. Tap to copy.

Images
docker pull <image>Download an image from a registry
docker build -t <name> .Build an image from the Dockerfile here
docker imagesList local images
docker rmi <image>Remove an image
Containers
docker run <image>Create and start a container from an image
docker run -d -p 8080:80 <image>Run detached and map a port
docker psList running containers
docker ps -aList all containers, including stopped
docker stop <id>Stop a running container
docker rm <id>Remove a stopped container
docker exec -it <id> bashOpen an interactive shell in a container
docker logs -f <id>Stream a container's logs
Compose
docker compose upStart the services in docker-compose.yml
docker compose up -dStart services in the background
docker compose downStop and remove the compose services
docker compose buildBuild or rebuild compose services
docker compose logs -fFollow logs for all services
Cleanup
docker system pruneRemove unused containers, networks and images
docker volume lsList volumes
docker network lsList networks

The core loop

Docker has a lot of subcommands, but daily work revolves around a short cycle: build an image from a Dockerfile, run a container from that image, and ps to see what's running. The mental model that makes Docker click: an image is a frozen snapshot of an app and everything it needs, and a container is a running instance of that image. Once that distinction is clear, the commands stop feeling arbitrary.

Getting inside and debugging

The commands people reach for when something's wrong are worth knowing cold. docker logs <id> shows a container's output, and docker exec -it <id> bash drops you into a shell inside a running container so you can poke around as if you'd SSH'd in. docker ps -a reveals stopped containers too, which is often where the clue to a crash is hiding. These three turn Docker from a black box into something you can inspect.

Compose and cleanup

For anything with more than one moving part — an app plus a database, say — docker compose up -d brings the whole stack up in the background from a single YAML file, and docker compose down tears it back down. One housekeeping note: images, stopped containers and volumes pile up and quietly eat disk space, so docker system prune is the periodic cleanup worth remembering. These pair naturally with the kubectl commands when you move from single containers to orchestration, and the Linux commands for everything around them.

FAQ

How do I get a shell inside a running container?
Run docker exec -it <container-id> bash (or sh if bash isn't available) to open an interactive shell.
How do I free up disk space used by Docker?
Run docker system prune to remove unused containers, networks and dangling images. Add -a to also remove unused images.

More cheatsheets

Helm commands
install, upgrade & charts
Terraform commands
init, plan, apply & state