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.
docker run -d --name my-api -p 3000:3000 my-image:latestChecking 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.
docker ps
docker ps -aReading 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.
docker logs my-api
docker logs -f my-apiRunning 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.
docker exec -it my-api /bin/shStopping 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.
docker stop my-api
docker rm my-api
# or combined:
docker rm -f my-apiCleaning 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.
docker system prune
docker system prune -a # more aggressive: removes all unused images tooCommon 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.
Related Tools
Related Guides
npm, Yarn, and pnpm Command Reference
The equivalent commands across npm, Yarn, and pnpm for everyday package management tasks — install, add, remove, run scripts, and update — side by side.
How to Check DNS and SSL Before Launching a Website
A pre-launch checklist for verifying DNS records and SSL certificates so a new domain or migrated site doesn't go live with broken email, mixed content warnings, or an expired certificate.
Last reviewed: 2026 · Written and verified by the Dev Utilities team · Editorial policy
Report an issue with this guide →