7

I created a docker compose *.yml file where I have many services with specified image tags. Then I let docker deploy a stack for me (on my local machine) with docker stack deploy -c .\my-compose-file.yml --with-registry-auth dev and it is able to run all services. When I have docker events running simultaneously, I can see image pull messages in the log, so docker pulls missing images. But when I run docker image ls -a, the pulled images are not displayed here.

So I wondering and want to know, what live cycle do the downloaded images have (will then be removed from my drive when I do docker stack rm or not), and when not, how do I clean up such images?

Simon Achmüller
  • 1,096
  • 13
  • 26

1 Answers1

1

I assume you have multi-node swarm configured. In such cases, the docker image ls is running in your local machine only, while the containers from the stack are distributed across nodes. The images are pulled on the nodes that will run the container.

To get the list of the containers, you will need to go to each member of the swarm and issue the command. Easy way to do it is to use, assuming you have ssh access to the nodes with identity key:

docker node ls | cut -c 31-49 | grep -v HOSTNAME | xargs -I"SERVER" sh -c "echo SERVER; ssh SERVER /usr/local/bin/docker image ls -a"
jordanvrtanoski
  • 5,104
  • 1
  • 20
  • 29
  • No, I observed such behavior also on a single node swarm. I deploy a stack on my local machine (only one in the swarm) and I don't see images pulled on my machine, that is what confused me. – Simon Achmüller Feb 22 '21 at 18:40
  • You can verify if the images are on your machine by inspecting the filesystem where Docker stores the metadata of the overlay images. It's usually in `/var/lib/docker/image/overlay2/` on linux machines. The `docker system prune -a --volumes` will delete all unused items. – jordanvrtanoski Feb 22 '21 at 19:38
  • Of course, images pulled during a swarm deployment will still end up in the local image chache. They will have the expected `Image ID`, though swarm will remove the `TAG` resulting in `` beeing shown. – Metin Mar 30 '21 at 20:31