All articles
Tutorials

15 Docker Commands Every Developer Should Know (With Examples)

Learn how to run, debug, build, and clean up Docker workloads with the 15 CLI commands developers reach for most, grouped by workflow with copy-paste examples.

15 Docker Commands Every Developer Should Know (With Examples) cover
9 min read

TL;DR

  • Fifteen commands cover most daily Docker work: run, ps, stop, start, rm for container lifecycle; exec, logs, inspect for debugging; build, images, pull, rmi for images; compose up, compose down for multi-service apps; system prune for disk recovery.
  • Reach for docker run when you need a new container, docker start when you are resuming one that already exists, and docker compose when the app has more than one service.
  • The failure mode is not missing obscure flags. It is using docker run for everything, never pruning disk, and debugging production issues without docker logs and docker inspect.
  • Modern Docker uses the docker compose plugin (V2). The standalone docker-compose binary is legacy; examples below use the integrated command.

You cloned a repo with a Dockerfile and a compose.yml, ran one command from a blog post three months ago, and now you are staring at Error response from daemon: Conflict. The container name "/api" is already in use with no idea whether to remove, restart, or rebuild. Docker's CLI has hundreds of subcommands. You do not need hundreds. You need the small set that shows up in every standup, incident, and local dev session.

In this article, you will get 15 Docker commands ranked by how often working developers actually use them, with copy-paste examples, the flags that matter, and the mistakes each command helps you avoid.

Note

What you need: Docker Engine 24+ or Docker Desktop 4.25+ with the Compose V2 plugin (docker compose version should work). All examples assume a Linux or macOS shell. On Windows, use WSL2 or Git Bash for identical syntax.

Why most Docker cheat sheets fail you

Most cheat sheets list 40 commands alphabetically. That is fine as a poster. It is useless when a container exits immediately and you need logs, or when disk usage hits 100% and Docker refuses to build.

The 15 commands below are ordered by workflow, not alphabet. They are the overlap across Docker's official docs, production cheat sheets, and the commands teams re-type daily: lifecycle first, then debugging, then images, then Compose, then cleanup. If you internalize these, you can handle a new microservice repo in minutes instead of re-googling flags.

WorkflowCommandsWhat you are trying to do
Run containersrun, ps, stop, start, rmCreate, list, pause, resume, and delete containers
Debug containersexec, logs, inspectShell in, read output, read metadata
Manage imagesbuild, images, pull, rmiBuild, list, download, and delete images
Multi-service appscompose up, compose downStart and stop stacks from YAML
Disk recoverysystem pruneReclaim space from orphaned objects

Commands for running containers

These five commands cover the container lifecycle from first launch to removal.

1. docker run

docker run creates and starts a container from an image. It is the command you use when nothing exists yet.

The example below runs Nginx detached, maps host port 8080 to container port 80, names the container, and auto-removes it when it stops:

docker run -d \
  --name web \
  -p 8080:80 \
  --rm \
  nginx:1.25-alpine

Common flags worth memorizing:

  • -d: detached (background)
  • -p HOST:CONTAINER: publish a port to your machine (see ports vs expose for when this differs from internal-only networking)
  • --name: stable name for stop, logs, and exec
  • --rm: delete the container on exit (great for one-off tests)
  • -e KEY=val / --env-file .env: inject environment variables
  • -v HOST_PATH:CONTAINER_PATH: bind-mount code or data for local dev

2. docker ps

docker ps lists running containers. Add -a to include stopped containers, which is what you need when something exited and you want its name back.

docker ps
docker ps -a --filter "name=web"

Use --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" when the default table hides the port mapping you care about.

3. docker stop

docker stop sends SIGTERM and waits for graceful shutdown before SIGKILL. Use it instead of deleting a container when you plan to start it again.

docker stop web
docker stop -t 30 web   # wait up to 30 seconds before SIGKILL

4. docker start

docker start resumes a stopped container with the same filesystem, env, and name. It does not re-read your image or Dockerfile.

docker start web
docker start -a web     # start and attach stdout/stderr to your terminal

5. docker rm

docker rm deletes a stopped container. You cannot remove a running container unless you pass -f.

docker rm web
docker rm -f web        # force-remove a running container
docker container prune  # remove all stopped containers (narrower than system prune)

Commands for debugging containers

When a container crashes on boot or behaves differently in Docker than on your host, these three commands are the first line of defense.

6. docker exec

docker exec runs a command inside a running container. The -it flags give you an interactive shell.

docker exec -it web sh
docker exec web nginx -t   # run a one-off command without a shell

If exec fails with "container is not running," check docker ps -a and read docker logs first. The container likely exited before you tried to shell in.

7. docker logs

docker logs prints stdout and stderr from the container's main process. This is how you see stack traces, bind errors, and migration failures.

docker logs web
docker logs -f --tail 100 web   # follow the last 100 lines live
docker logs --since 10m web     # only the last 10 minutes

8. docker inspect

docker inspect returns full JSON metadata: IP address, mount points, env vars, network mode, and health check state. Use --format to pull one field without scrolling.

docker inspect web
docker inspect --format '{{.State.Status}}' web
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web

Commands for building and shipping images

Images are the artifact docker run consumes. These four commands cover local builds and registry workflows.

9. docker build

docker build creates an image from a Dockerfile in the build context (usually .).

docker build -t myapp:local .
docker build -t myapp:local -f Dockerfile.prod .
docker build --no-cache -t myapp:local .   # ignore layer cache

Tag with a registry prefix before you push: myregistry.example.com/myapp:v1.0.

10. docker images

docker images lists images on your machine. Use it before rmi so you do not delete the wrong tag.

docker images
docker images myapp
docker images --filter "dangling=true"   # untagged intermediate layers

11. docker pull

docker pull downloads an image from a registry (Docker Hub by default). Pin a digest or version tag instead of latest in production pipelines.

docker pull nginx:1.25-alpine
docker pull myregistry.example.com/myapp:v1.0

12. docker rmi

docker rmi removes a local image. It fails if a running container still references that image unless you remove the container first.

docker rmi myapp:local
docker rmi -f myapp:local    # force when multiple tags point at the same layer
docker image prune -a        # remove images not used by any container

Commands for multi-container apps

Single-container tutorials hide the reality of modern apps: an API, a database, and a cache on a shared network. Compose encodes that stack in YAML so docker compose up replaces five manual docker run commands.

13. docker compose up

docker compose up reads compose.yml (or docker-compose.yml) and creates networks, volumes, and containers for every service.

docker compose up -d              # detached
docker compose up -d --build        # rebuild images before start
docker compose logs -f api        # tail one service after startup

14. docker compose down

docker compose down stops and removes containers, networks, and the default network created for the project. Add -v only when you intend to delete named volumes (database data).

docker compose down
docker compose down -v            # also remove named volumes (destructive)

Commands for reclaiming disk

Docker's layer cache and stopped containers accumulate fast on laptops and CI runners. When docker build fails with "no space left on device," this is the command you need.

15. docker system prune

docker system prune removes stopped containers, unused networks, dangling images, and build cache.

docker system prune               # interactive prompt
docker system prune -f            # skip prompt
docker system prune -a --volumes  # aggressive: unused images + volumes

Start with plain docker system prune. Escalate to -a only when you accept re-pulling images on the next build.

How to verify the workflow

Run this sequence once on a throwaway project to confirm the commands connect:

  1. docker pull nginx:1.25-alpine
  2. docker run -d --name verify-nginx -p 8080:80 nginx:1.25-alpine
  3. docker ps shows verify-nginx with 0.0.0.0:8080->80/tcp
  4. curl -I localhost:8080 returns HTTP headers from Nginx
  5. docker logs verify-nginx shows the Nginx access log line from your curl
  6. docker stop verify-nginx && docker rm verify-nginx

If you have a compose.yml in a repo, replace steps 2 through 6 with docker compose up -d, docker compose ps, docker compose logs, and docker compose down.

When these commands are not enough

These 15 commands assume single-host Docker Engine. They do not cover:

  • Kubernetes: kubectl replaces most of this CLI once workloads move to a cluster. Docker Desktop still uses these commands locally before you push images.
  • Multi-platform builds: docker buildx build --platform linux/amd64,linux/arm64 is required when Apple Silicon laptops build images for AMD64 servers.
  • Secrets and production hardening: Swarm secrets, rootless Docker, and image signing live outside daily dev workflows but matter in production.
  • Root cause inside the app: docker logs shows that the process crashed. It does not replace a debugger inside your language runtime.

For networking decisions that affect -p and internal service communication, read Docker ports vs expose next.

For authoritative reference, see the Docker CLI documentation and the Compose file reference.

Frequently asked questions

Share𝕏

Writer

  • Ilyas Rufai

    Technical content writer and DevSecOps specialist focused on cloud-native security and developer experience

Need help with your technical content?

We help B2B SaaS teams turn complex products into clear documentation and content that developers actually use.

Book a call
15 Docker Commands Every Developer Should Know | Reclear