October 4, 2026 · Yunus Emre Vurgun

What Are the Essential Docker Commands?

devops · cli · cheat-sheet · reference

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.

CommandWhat it does
docker run -d -p 8080:80 --name web nginxStart nginx detached, mapping host port 8080 to container port 80
docker run -it ubuntu bashStart an interactive shell; -i keeps stdin open, -t allocates a terminal
docker run --rm -v $(pwd):/app node:20 npm testRun tests with the project mounted, removing the container on exit
docker psList running containers with ports and names
docker ps -aList all containers, including stopped ones
docker stop webGracefully stop the container named web
docker start -a webRestart a stopped container, attaching output
docker rm webDelete 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.

CommandWhat it does
docker logs webShow the container stdout and stderr history
docker logs -f --tail 100 webFollow the last 100 lines live, like tail -f
docker exec -it web shOpen a shell inside the running container
docker inspect webDump full JSON config: mounts, network, env, entrypoint
docker statsLive CPU, memory, and network usage per container
docker top webList 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 image

Keep 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.

CommandWhat it does
docker volume create pgdataCreate a named volume for persistent data
docker run -d -v pgdata:/var/lib/postgresql/data postgres:16Run Postgres with its data on the volume
docker volume lsList volumes and their drivers
docker network create backendCreate an isolated network for a service tier
docker run -d --network backend --name api myappAttach a container so peers reach it as api
docker compose up -dStart every service in compose.yaml detached
docker compose logs -f apiFollow logs for one compose service
docker compose down -vStop 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 images

docker 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.