>_ IzzyLab

Hands-on task

This task is step 5 of the course “Docker from scratch”

Remove containers

easy~6 min

Clean things up:

  1. Remove the containers old-web (it's still running), test-1 and test-2. The db container is needed — it must stay and keep running.
  2. Start a container scratch in the background from the busybox:1.36 image with the command sleep 3600 so that it removes itself once stopped. You don't need to stop it — leave it running.
What you'll practise:
  • docker rm
  • docker rm -f
  • docker run --rm

Topic breakdown

A stopped container takes up space: its files, logs and settings are kept until the container is removed. docker rm name removes it — together with everything the process wrote inside the container (that's why important data is kept outside; more on that later).

docker rm won't remove a running container; it says you need to stop it first. docker rm -f stops and removes in one go — handy, but with no "are you sure?" prompt.

For one-off runs there's --rm: the container removes itself as soon as it stops.

docker run --rm busybox:1.36 date   # prints the date — and the container is already gone

What's checked

  1. old-web is removed, and db is running as before
  2. test-1 and test-2 are removed
  3. scratch from busybox:1.36 is running and will remove itself once stopped

What it looks like