CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Docker / docker rm

docker rm

Remove containers

Beginnercontainerscleanuplifecycle

docker rm deletes stopped containers: their writable layer, config and logs. It refuses running containers unless you force it, and it leaves images and named volumes alone, so removing a container never deletes your database's data volume by accident.

Syntaxdocker rm [-f] [-v] CONTAINER [CONTAINER...]

Watch it run

Yesterday's one-off containers are still around and their names block new ones.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
docker rm db-migrate api-old

Each removed container's name is printed. Their writable layers and logs are gone; the images they used are still there.

Anatomy of a command

$
commanddocker rm

Delete one or more containers. Same as docker container rm.

How it works

The CLI sends DELETE /containers/<id> (adding force=1 and v=1 for the flags). dockerd refuses if the container is running and force isn't set; with force it kills it first. Then it removes the container's network endpoints, unmounts and deletes its writable snapshot (everything the container wrote outside volumes), deletes /var/lib/docker/containers/<id> with the config and JSON log, and frees the name. The image is untouched. Volumes are reference-counted objects of their own: anonymous volumes (from VOLUME in the Dockerfile or -v /path) are deleted only with -v, and named volumes only by docker volume rm. That's why docker rm doesn't free the disk space a database has used.

Options you'll actually use

-f, --forceKill a running container (SIGKILL) and remove it
-v, --volumesAlso remove the container's anonymous volumes
-l, --linkRemove a legacy --link, not the container
docker container pruneRemove all stopped containers (with confirmation)
prune --filter until=24hOnly those created more than 24 hours ago
docker run --rmRemove the container automatically when it exits

Common mistakes

docker rm -f $(docker ps -aq) on a shared host

Kills and deletes every container on the machine, including other people's and the databases.

Target by name or label: docker rm $(docker ps -aq --filter label=job=ci)
Expecting docker rm to free a database's disk space

Named volumes are separate objects and are never removed by docker rm.

docker volume ls, then docker volume rm <name> once you're sure
Forgetting -v (or --rm) for throwaway containers from images with VOLUME

Each run leaves an anonymous volume behind; they pile up silently.

docker run --rm ... (removes anonymous volumes too), or docker rm -v

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Remove all exited containers with the label job=ci, in a way that does nothing when there are none (there are none here: check it doesn't error).

  2. 2

    Practise the xargs -r safety idea in the sandbox: find any *.tmp files in ~/data and delete them, without running rm when there are none.

  3. 3

    Write the command to remove a stopped container named scratch-db together with its anonymous volumes.

Related commands

docker stopStop a container gracefullydocker volumeManage persistent data for containersdocker system pruneReclaim disk space from unused Docker datadocker psList containers and their state
Previousdocker stopNextdocker inspect
Linux / Docker / docker rm

docker rm

Remove containers

Beginnercontainerscleanuplifecycle

docker rm deletes stopped containers: their writable layer, config and logs. It refuses running containers unless you force it, and it leaves images and named volumes alone, so removing a container never deletes your database's data volume by accident.

Syntaxdocker rm [-f] [-v] CONTAINER [CONTAINER...]

Watch it run

Yesterday's one-off containers are still around and their names block new ones.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
docker rm db-migrate api-old

Each removed container's name is printed. Their writable layers and logs are gone; the images they used are still there.

Anatomy of a command

$
commanddocker rm

Delete one or more containers. Same as docker container rm.

How it works

The CLI sends DELETE /containers/<id> (adding force=1 and v=1 for the flags). dockerd refuses if the container is running and force isn't set; with force it kills it first. Then it removes the container's network endpoints, unmounts and deletes its writable snapshot (everything the container wrote outside volumes), deletes /var/lib/docker/containers/<id> with the config and JSON log, and frees the name. The image is untouched. Volumes are reference-counted objects of their own: anonymous volumes (from VOLUME in the Dockerfile or -v /path) are deleted only with -v, and named volumes only by docker volume rm. That's why docker rm doesn't free the disk space a database has used.

Options you'll actually use

-f, --forceKill a running container (SIGKILL) and remove it
-v, --volumesAlso remove the container's anonymous volumes
-l, --linkRemove a legacy --link, not the container
docker container pruneRemove all stopped containers (with confirmation)
prune --filter until=24hOnly those created more than 24 hours ago
docker run --rmRemove the container automatically when it exits

Common mistakes

docker rm -f $(docker ps -aq) on a shared host

Kills and deletes every container on the machine, including other people's and the databases.

Target by name or label: docker rm $(docker ps -aq --filter label=job=ci)
Expecting docker rm to free a database's disk space

Named volumes are separate objects and are never removed by docker rm.

docker volume ls, then docker volume rm <name> once you're sure
Forgetting -v (or --rm) for throwaway containers from images with VOLUME

Each run leaves an anonymous volume behind; they pile up silently.

docker run --rm ... (removes anonymous volumes too), or docker rm -v

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Remove all exited containers with the label job=ci, in a way that does nothing when there are none (there are none here: check it doesn't error).

  2. 2

    Practise the xargs -r safety idea in the sandbox: find any *.tmp files in ~/data and delete them, without running rm when there are none.

  3. 3

    Write the command to remove a stopped container named scratch-db together with its anonymous volumes.

Related commands

docker stopStop a container gracefullydocker volumeManage persistent data for containersdocker system pruneReclaim disk space from unused Docker datadocker psList containers and their state
Previousdocker stopNextdocker inspect