>_ IzzyLab

Hands-on task

This task is step 8 of the course “Docker: everyday work”

Cleanup: remove the clutter, keep what's needed

medium~8 min

The server is cluttered with containers. Only three are needed:

  • web and api, running;
  • db, stopped — it was stopped for maintenance: don't start it and don't remove it.

Remove all the other containers — both running and stopped. Don't recreate the three you need.

What you'll practise:
  • docker rm
  • docker container prune
  • docker ps --filter

Topic breakdown

Containers pile up fast: every docker run without --rm leaves a stopped container behind. For cleaning up there are prune commands:

  • docker container prune — remove all stopped containers;
  • docker image prune — untagged images; with -a — all images no container was created from;
  • docker system prune — everything at once: stopped containers, unused networks, untagged images, build cache.

They ask for confirmation (-f to skip it) and don't care whether you need a stopped container. That's why on a server where not everything is yours you look at the list first and remove things one by one:

docker ps -a --filter status=exited     # who's stopped
docker ps -aq --filter name=tmp-        # IDs only — handy to pass to docker rm

What's checked

  1. Only web and api are running — the same containers
  2. Of the stopped ones only db is left — the same one, still stopped

What it looks like