Skip to main content

How to Stop and Remove a Docker Container

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A stopped container isn't gone: Docker keeps it, with its name and its log, until you remove it. docker stop shuts it down, and docker rm deletes it. This guide stops an nginx container, finds it among the stopped ones, removes it, and shows --rm, which removes a container as soon as it stops, with real output from Docker Desktop 4.100.1.

Syntax​

Syntax
docker stop NAME
docker rm NAME
PartWhat it means
docker stop NAMEAsk the container's program to stop, and wait until it does
docker rm NAMEDelete a stopped container
docker ps -aList every container, running or stopped

The docker stop reference: "The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL." SIGTERM asks the program to shut down cleanly; SIGKILL ends it if it hasn't. The same commands work in PowerShell.

Prerequisites​

  • Docker: Docker Desktop on Mac or Windows, running.
  • A running container. This guide uses one named web, started with docker run -d --name web nginx:alpine.

Step 1: Stop the Container​

Terminal
docker stop web
Output
web

Docker printed the name once the container had stopped. It's still there:

Terminal
docker ps -a --filter name=web
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
540be37b12b6 nginx:alpine "/docker-entrypoint.…" 4 seconds ago Exited (0) Less than a second ago web

docker stop web printing web, and docker ps -a listing the web container with STATUS Exited (0) Less than a second ago.

Exited (0): nginx shut down cleanly, with exit code 0. -a matters here: plain docker ps shows only running containers.

Step 2: Remove It​

Terminal
docker rm web
docker ps -a --filter name=web
Output
web
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

docker rm web printing web, and docker ps -a now listing no container named web.

The container is gone, and the name web is free again. The image, nginx:alpine, stays.

Step 3: Let Docker Remove It for You​

--rm deletes the container as soon as its program ends. A container that only prints something:

Terminal
docker run --rm --name rmtest alpine:latest echo hello
docker logs rmtest
Output
hello
Error response from daemon: No such container: rmtest

It printed hello, stopped, and was removed straight away, so there's no log to read afterwards.

Common Mistakes​

MistakeWhat happensDo this instead
Starting a new container with the old name while the stopped one is still thereConflict. The container name "/web" is already in usedocker rm web first
--rm on a container whose log you need laterThe log goes with the containerLeave out --rm, and remove it yourself

Troubleshooting​

SymptomLikely causeFix
Error response from daemon: No such containerThe name is wrong, or it's already removeddocker ps -a
docker ps doesn't list a container you just stoppedPlain docker ps shows running containers onlydocker ps -a

FAQ​

Can I start a stopped container again?​

Yes, with docker start NAME, as long as you haven't removed it. Its earlier log is kept.

Does removing a container delete the image?​

No: docker rm deletes the container only. Images are removed with docker rmi: How to Remove a Docker Image.

Conclusion​

docker stop web shut nginx down cleanly, Exited (0), docker ps -a still listed it, and docker rm web deleted it and freed its name. docker run --rm removed a container by itself the moment it stopped.

Additional Resources​