You don't need to memorize Docker's full command set. These commands cover almost everything you'll do day to day, and the troubleshooting steps at the end handle most containers that won't start.
Learning Objectives
- Pull, run, list, stop, and remove images and containers.
- Read a container's logs and inspect its details.
- Debug a container that exits right away, step by step.
The Commands
| Command | What it does |
|---|---|
| docker --version | Shows the installed Docker version |
| docker pull nginx | Downloads an image from a registry |
| docker images | Lists images on your machine |
| docker run -d --name web -p 8080:80 nginx | Starts a container in the background, mapping port 8080 to 80 |
| docker ps | Lists running containers |
| docker ps -a | Lists all containers, including stopped ones |
| docker stop web | Stops a running container |
| docker start web | Starts a stopped container |
| docker rm web | Removes a stopped container |
| docker rmi nginx | Removes an image no container is using |
| docker logs web | Shows what the container printed |
| docker exec -it web sh | Opens a shell inside a running container |
| docker inspect web | Shows the container's full configuration as JSON |
exec -it web sh uses sh because it exists in nearly every image. bash is missing from many small images such as Alpine.
Debugging a Container That Won't Start
Logs answer most questions. A missing environment variable, a wrong command, or an app that crashes on startup will all show up there. Fix the cause, then start the container again.
Check the Status
docker ps -a
Read the Logs
docker logs <name>
Inspect the Details
docker inspect <name>
Fix and Restart
docker start <name>
Warning: The Cleanup Command Deletes More Than You Expect
docker system prune -a removes every stopped container, every network not in use, and every image not used by a container, not only the broken ones. Run docker system df first to see how much space is used, and read the prompt before confirming. Volumes are not removed unless you add --volumes.
Learning Check
Why does docker ps not show my container after it crashed?
docker ps only lists running containers. Use docker ps -a to see stopped or crashed ones.
What is the first command to run when a container won't start?
docker logs followed by the container name. The output usually names the problem.
Step by Step: Clean Up Safely
- See how much space Docker uses: docker system df.
- Remove only stopped containers: docker container prune. It asks before deleting.
- Remove unused images only after reading what will go: docker image prune. Add -a only when you're sure.
- Check the result with docker system df again.
Common Mistakes
Trying to remove a running container without stopping it
docker rm fails on a running container. Stop it first, or use docker rm -f to force it.
Running prune commands without reading the prompt
docker system prune -a removes all unused images, not only broken ones. Read the summary before confirming.
Guessing at a problem instead of reading the logs
docker logs shows what the program printed. Start there before changing the Dockerfile or the command.
Interview Questions
What's the difference between docker stop and docker kill?
stop sends a termination signal and waits for the process to exit cleanly before forcing it. kill stops it immediately.
How do you see why a container exited?
Run docker ps -a to see its status and exit code, then docker logs to read the output.
What is the difference between docker run and docker start?
run creates a new container from an image and starts it. start restarts an existing, stopped container without creating a new one.
Summary
The core commands are pull, run, ps, stop, start, rm, logs, exec, and inspect. When a container won't start, check its status, then its logs, then its details, and be careful with the cleanup commands.