Docker is an essential tool for developers, but even experienced users can make mistakes that lead to wasted time, confusion, or even data loss. I'll cover four common Docker command mistakes and how to fix them, so you can avoid unnecessary headaches.
How To Follow Along
Before we dive into the mistakes, let’s set up a test environment so you can follow along.
Run these commands in your terminal to create some sample containers:
# Pull test images
docker pull nginx
docker pull alpine
# Start an Nginx container in detached mode
docker run -d --name test_nginx nginx
# Start an Alpine Linux container for later demos
docker run -it --name test_alpine alpine sh
Now, let’s look at the four most common mistakes and how to avoid them.
Losing Containers in Detached Mode
The Mistake
Many users start a container with the -d
flag (detached mode) and wonder why they don’t see any output:
docker run -d nginx
The terminal returns a container ID, but there’s no indication that the container is running, leading to confusion.
The Fix
To check the container’s output, use:
docker ps # See running containers
docker logs <container_id>
If you need an interactive session for debugging, run the container without -d
:
docker run -it nginx
Confusing docker stop
& docker kill
The Mistake
A common misunderstanding is using docker kill
instead of docker stop
:
docker kill test_nginx
If you try restarting the container after using docker kill
, you might run into unexpected issues:
docker start test_nginx
The problem? docker kill
forcefully stops the container without a graceful shutdown.
The Fix
Instead, use docker stop
, which sends a termination signal allowing processes to exit cleanly:
docker run -d --name test_nginx nginx # Restart if needed
docker stop test_nginx
docker start test_nginx
Use docker kill
only when a container is unresponsive and needs to be stopped immediately.
Starting a Bash Session Without Bash Installed
The Mistake
When trying to open a shell inside a lightweight container like Alpine Linux, many users assume bash
is available:
docker exec -it test_alpine bash
Error: "bash not found"
The reason? Some minimal Linux images (like Alpine) don’t include bash
by default.
The Fix
Instead, use sh
(or ash
in Alpine):
docker exec -it test_alpine sh
For Debian-based images, bash
is usually available. Always check the image type before assuming bash
exists.
Running docker system prune
The Mistake
Many users run the following command without realizing its full impact:
docker system prune
This deletes ALL unused images, containers, networks, and volumes. If you expected it to only remove stopped containers, you might end up deleting critical resources.
The Fix
To remove only stopped containers, use:
docker container prune
This ensures you don’t lose important data unintentionally.
Video Walkthrough
Wrapping Up
These four common Docker command mistakes are simple to fix but left unchecked, they can easily lead to confusion, lost time, and even accidental data loss.
By understanding these commands properly, you'll be ready to troubleshoot issues, stop containers safely, and clean up disk space without risk.