Suggest an editImprove this articleRefine the answer for “How to stop and remove a Docker container?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`docker stop`** halts a running container; **`docker rm`** deletes it. Combine with `-f` to do both in one command, or `--rm` on `docker run` to auto-clean on exit. ```bash docker stop web # graceful (SIGTERM, then SIGKILL after 10s) docker rm web # delete (must be stopped first) docker rm -f web # stop AND remove docker rm -fv web # also delete anonymous volumes docker rm -f $(docker ps -aq) # nuke ALL containers docker container prune -f # delete all stopped containers ``` **Key:** stop pauses the process; rm deletes the container record. Use `-v` on `rm` to also drop anonymous volumes (named volumes are spared). Use `--rm` on `docker run` for one-off commands that clean up after themselves.Shown above the full answer for quick recall.Answer (EN)Image**Stopping and removing containers** is the cleanup half of the lifecycle. The two operations are separate by design: `stop` pauses, `rm` deletes. Knowing how to combine them and how to bulk-clean is daily ops hygiene. ## Theory ### TL;DR - `docker stop <name>` = graceful shutdown (SIGTERM, 10-second grace, then SIGKILL). - `docker kill <name>` = immediate (SIGKILL, no grace). Use only on hung processes. - `docker rm <name>` = delete the container's metadata + writable layer. Container must be stopped first. - `docker rm -f <name>` = combined stop + remove (sends SIGKILL, no graceful). - `docker rm -v <name>` = also delete anonymous volumes. **Named volumes are NEVER auto-deleted** (intentional safety). - `docker container prune` = bulk-delete all stopped containers. - `docker run --rm ...` = mark a container for auto-removal when it exits. Best for one-off commands. ### Quick example ```bash # Run a container $ docker run -d --name web nginx:1.27-alpine # Stop gracefully $ docker stop web web # (took ~0s — nginx handles SIGTERM cleanly) # Container still exists, just stopped $ docker ps -a --filter name=web STATUS NAMES Exited (0) 2 seconds ago web # Now remove $ docker rm web web # Or do it all in one go next time $ docker run -d --name web nginx:1.27-alpine $ docker rm -f web # stop + remove combined ``` The two-step is verbose but explicit. The combined `rm -f` is what most ops scripts use. ### `stop` vs `kill` vs `rm -f` ```bash docker stop <name> # SIGTERM, wait 10s, SIGKILL if needed docker stop -t 30 <name> # custom grace period (30s) docker kill <name> # immediate SIGKILL (or --signal=SIGUSR1, etc.) docker rm -f <name> # SIGKILL + remove (no grace at all) ``` **For databases:** always `docker stop` (or just stop the running app inside, then rm). Never `kill` or `rm -f` on a running database — you skip the WAL flush. **For broken containers / debugging:** `kill` or `rm -f` is fine. **For services:** `stop` is the default; the app should handle SIGTERM gracefully (flush, close connections, exit 0). ### Volume cleanup with `-v` ```bash # Default: anonymous volumes orphan, named volumes spared $ docker rm web # With -v: anonymous volumes also deleted $ docker rm -v web # Named volumes are STILL not touched. ``` Anonymous volumes (no name, auto-generated when an image's `VOLUME` directive runs without `-v`) are easy to leak. Use `-v` on `rm` to clean them up. Named volumes are intentionally protected; you have to `docker volume rm` them explicitly. ### Bulk operations ```bash # Stop everything docker stop $(docker ps -q) # Remove all stopped containers docker container prune # asks for confirmation docker container prune -f # no prompt # Remove EVERYTHING (running gets force-killed) docker rm -f $(docker ps -aq) # Filtered cleanup: stopped > 24h ago docker container prune --filter 'until=24h' -f # Stop + prune in compose docker compose down # stops + removes containers + project network docker compose down -v # also wipes named volumes (DESTRUCTIVE) ``` ### `--rm` for self-cleaning runs ```bash # One-off task: container exits, container is removed $ docker run --rm alpine echo hi hi # No container left behind in `docker ps -a`. # Useful in CI: $ docker run --rm -v $PWD:/work node:22 npm test ``` `--rm` is the single best flag for keeping a developer machine clean — every test, build, or one-off you run vanishes when done. ### Common mistakes **`rm` on a running container without `-f`** ```bash $ docker rm web Error response from daemon: cannot remove container "web": container is running: stop the container before removing or force remove # Fix: stop first, OR use -f $ docker stop web && docker rm web # OR $ docker rm -f web ``` Docker refuses to delete a running container by default — protection against accidental destruction. **Forgetting `-v` and accumulating orphan volumes** ```bash $ docker rm web # anonymous volume orphaned $ docker volume ls -f dangling=true # find them $ docker volume prune -f # delete them ``` Over months, orphan anonymous volumes can take many gigabytes. `docker rm -v` at delete time prevents the orphan in the first place. **Using `rm -f` on a database mid-write** ```bash # WRONG: kills postgres without flush, may corrupt data on next start $ docker rm -f pg # RIGHT: stop gracefully so postgres can shut down cleanly $ docker stop pg && docker rm pg ``` Databases need their grace period. **Confusing `docker stop` with `docker pause`** ```bash docker stop # → exited (process gone, can restart) docker pause # → paused (process frozen, can unpause) ``` Different operations. Pause is rare; stop is daily. ### Real-world usage - **Production deploys:** `docker stop old-container && docker rm old-container` (or rely on Compose / Swarm / K8s to do it). - **Local dev between sessions:** `docker compose down` (containers gone) or `docker compose stop` (containers preserved, faster restart). - **CI cleanup hooks:** `docker rm -f $(docker ps -aq) || true` at job end to prevent state leaking between jobs. - **Disk cleanup:** `docker container prune --filter 'until=24h' -f && docker image prune -f && docker volume prune -f`. ### Follow-up questions **Q:** What is the difference between `docker rm -f` and `docker kill && docker rm`? **A:** Functionally similar. `rm -f` does it in one step; the explicit pair lets you choose the signal (`docker kill -s SIGUSR1`) or watch the kill happen separately. Both result in immediate termination + removal. **Q:** Why does `docker stop` sometimes take 10 full seconds? **A:** Your app inside is not handling SIGTERM. Docker waits the grace period for graceful shutdown; if the app does not exit by itself, Docker sends SIGKILL. Fix: trap SIGTERM in your app code and exit cleanly. **Q:** How do I stop one Compose service without stopping the whole stack? **A:** `docker compose stop <service-name>` or `docker compose rm <service-name>`. Stops just that one; others keep running. **Q:** Does `docker rm` delete the image too? **A:** No. Image and container are separate. `docker rm` deletes the container (writable layer + metadata); the image stays in your local cache. To delete the image: `docker rmi <image>`. **Q:** (Senior) How do you safely stop a long-running stateful container that takes minutes to flush? **A:** Increase the grace period: `docker stop -t 300 dbcontainer` (5 minutes). Docker keeps SIGTERM-then-wait until that timeout. Make sure your app actually handles SIGTERM by initiating its drain/flush. For databases like Postgres, `docker stop` lets the postmaster do a fast or smart shutdown depending on config; check the image's docs. ## Examples ### Clean run with `--rm` ```bash $ docker run --rm -it alpine sh / # uname -a Linux 4f06b3e2c0c1 ... / # exit $ docker ps -a --filter name=alpine # (nothing — container auto-removed on exit) ``` `--rm` makes ad-hoc containers feel like running scripts. ### Restart a service cleanly ```bash $ docker stop web # graceful $ docker rm web # delete record + writable layer $ docker run -d --name web --restart=unless-stopped \ -p 8080:80 nginx:1.27-alpine # fresh container, same name ``` The canonical update flow for a single-container deploy. With Compose, `docker compose up -d` does the same comparison automatically (only changed services restart). ### Nuclear cleanup ```bash # Stop everything $ docker stop $(docker ps -q) # Remove all containers (running or not) $ docker rm -f $(docker ps -aq) # Reclaim disk: images, volumes, networks, build cache $ docker system prune -af --volumes Total reclaimed space: 8.4GB ``` Nuke everything. Useful before a clean state demo or when the host is full. The `--volumes` flag is the destructive one — only run it when you are sure.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.