Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Stop and Remove One Container

docker stop web
docker rm web

docker stop sends the image's stop signal to the main process (SIGTERM unless the image sets another; the Nginx image uses SIGQUIT), waits 10 seconds by default, and then sends SIGKILL. docker rm deletes the stopped container: its settings, its writable layer, and its logs. The image stays, and so do named volumes and bind-mounted folders.

Look Before You Remove

Removing a container deletes every file it wrote outside a volume or bind mount, and there is no undo. Check first:

docker ps -a
docker inspect -f '{{json .Mounts}}' web
docker volume ls

If the mount list is empty, the container keeps its data inside itself. Copy it out before you go on, for example with docker cp web:/var/www ./web-backup. Data that matters belongs in a named volume or bind mount and in a backup; see Docker volumes vs bind mounts and snapshot vs backup.

Give a Slow Process More Time

docker stop -t 60 db

A database may need more than 10 seconds to write its buffers and close its files. -t sets how many seconds Docker waits before it kills the process. Use docker kill only when docker stop does not work: it sends SIGKILL at once, as kill -9 does for a normal process; see how to kill a process in Linux.

Force Removal and Anonymous Volumes

CommandWhat it does
docker rm -f webKills and removes a running container
docker rm -v webAlso removes its anonymous volumes
docker rm web db cacheRemoves several at once

-f skips the clean stop and kills the process. -v deletes the anonymous volumes of the container: volumes with a long random name that Docker created because the image declares a volume path. They can hold real data. The official MySQL image, for example, declares /var/lib/mysql as a volume, so a database started without a named volume keeps its tables in an anonymous one, and rm -v deletes them. Named volumes are never removed by docker rm.

Stop and Remove All Containers

These commands act on every container on the host, including those of other projects, and there is no undo. Check the mounts of each container as shown above, then read the list:

docker ps -a
docker stop $(docker ps -q)
docker rm $(docker ps -aq)

docker ps -q prints the IDs of running containers, -aq the IDs of all of them. docker rm without -f refuses to remove a container that is still running. If the list is empty, Docker says the command requires at least one argument; nothing happens.

Remove All Stopped Containers

Prune has no undo either; check each stopped container's mounts before you run it.

docker ps -a --filter status=exited
docker container prune
docker container prune --filter "until=24h"

docker container prune deletes every stopped container after asking for confirmation; -f skips the question. The until filter keeps containers created in the last 24 hours. To clean images, networks, and build cache in the same step, see how to clean up disk space used by Docker.

Remove Automatically with --rm

docker run --rm -it alpine sh
docker run --rm -v "$PWD":/work -w /work node:26 npm test

--rm deletes the container and its anonymous volumes as soon as it exits. Use it for one-off tasks such as tests or a quick shell. It does not suit services, and it cannot be combined with a restart policy.

Containers Started with Compose

docker compose stop
docker compose down

Remove the containers of a Compose project with Compose itself, from the project folder; otherwise the next up recreates them. down removes the containers and the project network and keeps named volumes. down -v deletes those volumes too, as docker compose up -d explains.

Common Errors

  • Cannot remove a running container: stop it first, or use -f if a hard stop is acceptable.
  • Name already in use: a stopped container with that name still exists. Find it with docker ps -a, then remove or rename it.
  • Image removal fails: a container, even a stopped one, still uses the image. Remove the container first; see how to remove Docker images.
Was this answer helpful?
Loading...