Stop, Start and Remove Containers
A container has a lifecycle: it runs, it stops, it starts again, and it is removed. docker stop, docker start and docker rm drive each step.
Stop a container
This command stops the running container by name.
docker stop g3-webStart it again
This command starts the stopped container again.
docker start g3-webRemove it
Stop it first, then remove it by name.
docker stop g3-web
docker rm g3-webExample
docker run -d --name g3-web nginx:1.27-alpine >/dev/null
docker stop g3-web >/dev/null
docker start g3-web >/dev/null
docker ps --format "{{.Names}} {{.Image}}"Key points
- Use docker stop to stop a running container.
- Use docker start to start a stopped container again.
- Use docker rm to remove a stopped container.
- Use docker rm -f to stop and remove a running container at once.
- A stopped container keeps its files until it is removed.
In this example, Docker starts nginx, stops it, and starts it again. The last command lists the container by name and image. The row shows the same container again after its stop.
Note
Removing a container deletes its files. A volume keeps data past removal.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does docker rm -f do to a running container?