Search code examples
dockerdocker-swarm

How do I remove old service images after an update?


I'm toying around with Docker Swarm. I've deployed a service an performed a couple of updates to see how it works. I'm observing that docker is keeping the old images around for the service.

How do I clean those up?


root@picday-manager:~# docker service ps picday
ID            NAME          IMAGE                   NODE            DESIRED STATE  CURRENT STATE            ERROR  PORTS
bk6pw0t8vw4r  picday.1      ischyrus/picday:latest  picday-manager  Running        Running 6 minutes ago
ocbcjpnc71e6   \_ picday.1  ischyrus/picday:latest  picday-manager  Shutdown       Shutdown 6 minutes ago
lcqqhbp8d99q   \_ picday.1  ischyrus/picday:latest  picday-manager  Shutdown       Shutdown 11 minutes ago
db7mco0d4uk0  picday.2      ischyrus/picday:latest  picday-manager  Running        Running 6 minutes ago
z43p0lcdicx4   \_ picday.2  ischyrus/picday:latest  picday-manager  Shutdown       Shutdown 6 minutes ago

Solution

  • These are containers, not images. In docker, there's a rather significant difference between the two (images are the definition used to create a container). Inside of a swarm service, they are referred to as tasks. To adjust how many docker keeps by default, you can change the global threshold with:

    docker swarm update --task-history-limit 1
    

    The default value for this is 5.

    To remove individual containers, you can remove the container from the host where it's running with:

    docker container ls -a | grep picday
    docker container rm <container id>