October 4, 2026 · Yunus Emre Vurgun
What Are the Essential Docker Commands?
Docker wraps applications and their dependencies into portable containers built from images, and about twenty commands cover the daily workflow: run, inspect, log, exec, stop, build, and clean up. This cheat sheet lists each one with exactly what it does and the flags worth memorizing. Examples assume a project with a Dockerfile in the current directory.
Run and manage containers
Containers are running instances of images. These commands start, stop, and remove them; learn run first, since its flags reappear everywhere.
| Command | What it does |
|---|---|
docker run -d -p 8080:80 --name web nginx | Start nginx detached, mapping host port 8080 to container port 80 |
docker run -it ubuntu bash | Start an interactive shell; -i keeps stdin open, -t allocates a terminal |
docker run --rm -v $(pwd):/app node:20 npm test | Run tests with the project mounted, removing the container on exit |
docker ps | List running containers with ports and names |
docker ps -a | List all containers, including stopped ones |
docker stop web | Gracefully stop the container named web |
docker start -a web | Restart a stopped container, attaching output |
docker rm web | Delete a stopped container; add -f to force-remove a running one |
Prefer naming containers with --name so later commands read like English instead of hash prefixes. The -p host:container order trips up beginners — traffic arrives at the host port on the left and forwards to the container port on the right. For the concepts behind isolation and images, see the virtualization concepts catalog entry.
Inspect, log, and exec
When a container misbehaves, these are the debugging commands. Reach for logs before a shell: most failures announce themselves in the first lines of output.
| Command | What it does |
|---|---|
docker logs web | Show the container stdout and stderr history |
docker logs -f --tail 100 web | Follow the last 100 lines live, like tail -f |
docker exec -it web sh | Open a shell inside the running container |
docker inspect web | Dump full JSON config: mounts, network, env, entrypoint |
docker stats | Live CPU, memory, and network usage per container |
docker top web | List processes running inside the container |
docker cp web:/app/out.csv ./ | Copy a file out of the container to the host |
A reliable debugging order is logs, then inspect to check environment and mounts, then exec to poke around inside. If exec reports no shell, the image is probably distroless or scratch-based — use docker run --rm -it --entrypoint sh image to start a fresh copy with a shell instead, or debug from the Dockerfile side.
Build images and manage Dockerfiles
Images are built from Dockerfiles as stacked layers, and each instruction adds one layer. Order instructions from least to most frequently changing so rebuilds reuse cached layers.
docker build -t myapp:1.2 . # build from ./Dockerfile, tag 1.2
docker build --no-cache -t myapp . # rebuild ignoring the layer cache
docker images # list local images and sizes
docker tag myapp:1.2 reg.io/myapp:1.2 # retag for a registry
docker push reg.io/myapp:1.2 # upload the image
docker pull postgres:16 # download without running
docker rmi myapp:1.2 # delete a local imageKeep images small and builds fast with a few habits: pin base tags (node:20-slim, not node:latest), copy dependency manifests before source so installs stay cached, combine chained RUN steps with && to reduce layers, and add a .dockerignore so secrets and node_modules never enter the build context. Builds usually run in CI — the CI/CD pipeline stages entry shows where build, tag, and push fit.
Volumes, networks, and compose
Container filesystems are ephemeral: anything not in a volume disappears with the container. Volumes persist data, networks connect containers, and Compose declares the whole stack in one YAML file.
| Command | What it does |
|---|---|
docker volume create pgdata | Create a named volume for persistent data |
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16 | Run Postgres with its data on the volume |
docker volume ls | List volumes and their drivers |
docker network create backend | Create an isolated network for a service tier |
docker run -d --network backend --name api myapp | Attach a container so peers reach it as api |
docker compose up -d | Start every service in compose.yaml detached |
docker compose logs -f api | Follow logs for one compose service |
docker compose down -v | Stop the stack and delete its volumes |
Inside a user-defined network, containers resolve each other by name, so your app connects to postgres://db:5432/app instead of a hardcoded IP. Published ports still matter for host access — the common network ports reference helps you pick host ports that do not collide with services already running.
Why is my container exiting immediately?
Because a container lives only as long as its main process, and yours finished or crashed. Check docker logs first — a stack trace or missing-variable error is usually sitting there. Common causes: the command ran to completion (a bare ubuntu image with no -it exits at once), the app bound to localhost instead of 0.0.0.0 and the healthcheck killed it, a required environment variable was never passed with -e, or the entrypoint script has Windows line endings and fails to execute. Fix the process, not the container flags: add a long-running foreground command, bind all interfaces, pass the variables, and convert the script with dos2unix. If it still dies, run the image with --entrypoint sh and launch the entrypoint by hand to watch it fail.
Cleanup: reclaiming disk space
Stopped containers, dangling images, and stale build cache accumulate fast — tens of gigabytes on an active dev machine. Clean in escalating order so you never delete something a running container needs.
docker container prune # delete stopped containers
docker image prune # delete dangling (untagged) images
docker image prune -a # delete all images unused by containers
docker volume prune # delete volumes no container uses (careful!)
docker system df # show what is using space
docker system prune # containers, networks, dangling imagesdocker system prune is the everyday answer and is safe for running containers, but leave -a and --volumes for deliberate cleanups — rebuilding a deleted cache is slow, and pruned named volumes take your local databases with them. Pair container hygiene with source hygiene: the git commands cheat sheet covers keeping the code that goes into your images tidy.