Docker Command Reference for Developers

Docker's CLI surface is large, but daily development work uses a consistent, small set of commands: running a container, checking what's running, reading logs, and cleaning up afterward. This reference covers those, plus the cleanup commands that prevent disk space from silently filling up with unused images and containers.

Key Takeaways

  • docker ps shows running containers; docker ps -a shows all containers including stopped ones — the -a flag is easy to forget when a container "disappears"
  • -d runs a container in detached (background) mode; without it, the container's output ties up your terminal
  • docker logs -f follows a container's output live, the container equivalent of tail -f
  • Stopped containers and unused images accumulate disk space silently — periodic cleanup with prune commands is normal maintenance, not an emergency fix
  • docker exec runs a command inside an already-running container; docker run always creates a new container

Running a container

-d runs detached (in the background), -p maps a host port to a container port, and --name gives the container a memorable name instead of a random one. Without -d, the container runs in the foreground and its logs stream directly to your terminal until you stop it.

Run detached with a port mapping
docker run -d --name my-api -p 3000:3000 my-image:latest

Checking what's running

docker ps lists currently running containers. docker ps -a lists all containers, including ones that have stopped — the most common "where did my container go" confusion is forgetting that a stopped container still exists and just isn't shown by the plain docker ps.

List containers
docker ps
docker ps -a

Reading logs

docker logs <container> shows a container's accumulated output. Add -f to follow new output live, the same way tail -f works for a log file — this is the fastest way to watch a container behave in real time while debugging.

View and follow logs
docker logs my-api
docker logs -f my-api

Running a command inside a running container

docker exec runs an additional command inside a container that's already running — commonly used to open an interactive shell for debugging. This is different from docker run, which always creates and starts a brand-new container.

Open a shell in a running container
docker exec -it my-api /bin/sh

Stopping and removing containers

docker stop sends a graceful shutdown signal; docker kill forces an immediate stop. A stopped container still exists on disk (and still shows in docker ps -a) until you docker rm it — stop and remove are two separate steps.

Stop and remove
docker stop my-api
docker rm my-api

# or combined:
docker rm -f my-api

Cleaning up disk space

docker system prune removes stopped containers, unused networks, and dangling images; add -a to also remove unused images that aren't dangling (not just untagged ones). This is routine maintenance — Docker doesn't automatically reclaim disk space from old images and stopped containers on its own.

Clean up unused resources
docker system prune
docker system prune -a   # more aggressive: removes all unused images too

Common Mistakes to Avoid

  • Forgetting -a on docker ps and assuming a stopped container "disappeared" instead of just being hidden from the default view
  • Running a container in the foreground (no -d) during normal use, tying up a terminal unnecessarily
  • Using docker stop on a container that needs immediate termination, when docker kill is the appropriate tool for that case
  • Never running cleanup commands and being surprised when disk space fills up with old stopped containers and unused images
  • Confusing docker exec (run a command in an already-running container) with docker run (always creates a new container)

Frequently Asked Questions

Why doesn't my stopped container show up in docker ps?

docker ps only shows currently running containers by default. Use docker ps -a to see all containers, including stopped ones — the container still exists on disk until you explicitly remove it with docker rm.

What's the difference between docker stop and docker kill?

docker stop sends a termination signal and gives the container time to shut down gracefully before forcing it. docker kill terminates immediately with no grace period — use stop by default, and kill only when a container is unresponsive or you need it gone right away.

Do I need to manually clean up Docker disk usage?

Yes — Docker does not automatically remove stopped containers or unused images on its own. Periodically running docker system prune (or the more aggressive -a variant) is normal maintenance, not a sign something went wrong.

Last reviewed: 2026 · Written and verified by the Dev Utilities team · Editorial policy

Report an issue with this guide →