How do I stop and remove Docker containers?
Stop a single container
Use docker ps to see running containers and docker ps -a to include stopped ones. Stop a container by name or ID with docker stop. Docker sends a stop signal and waits a short time before forcing it, so the app has a chance to shut down cleanly.
Stopping does not delete the container. It still exists and can be started again with docker start. Removing it is a separate step that frees the name and its writable layer.
- docker stop myapp
- docker rm myapp removes a stopped container
- docker rm -f myapp stops and removes in one step
- docker container prune removes all stopped containers
Clean up carefully
To stop every running container, you can use docker stop $(docker ps -q). To remove every container, including running ones, use docker rm -f $(docker ps -aq). Run these only when you are sure nothing important is running, since they affect every container on the host.
Data in named volumes survives container removal, but data written inside a container without a volume is lost with it. Check docker volume ls before cleaning up, and only use docker system prune when you know what it deletes.
Common mistakes
- Running docker rm -f on every container on a shared server without checking first.
- Expecting data to survive when it was stored only inside the container.
- Confusing stopping a container with removing it.
