Ansible has a nice example how to scale a container to n instances using with_sequence:
- name: Start 4 load-balanced containers
community.docker.docker_container:
name: "container{{ item }}"
recreate: yes
image: someuser/anotherappimage
command: sleep 1d
with_sequence: count=4
This allows me to update the count as desired and thus scale up my service. When scaling down (e.g. count=4 → count=3) however, the containers which are no longer used will just continue running.
All I can imagine for now to perform the clean up would be to list all containers, filter, and then remove them one by one, which seems rather crude considering the simplicity of above’s snippet -- is there a simpler way which I'm missing here?