I need to run a command against multiple running containers. For example, let's say my app needs to run a data structure update against a database after receiving code updates. To do this, we want to run docker build <project_dir> -t latest
, then docker stop; docker rm; docker run
. At this stage we can assume we have updated the container's core code, but we still need to run that database update using the app's own tooling.
Essentially, I need some way to get a list of running containers, filtered by some criteria, and register those container IDs with Ansible. Then for each container we run the command.
Like this:
- name: Get list of running containers
docker:
image: my-image:latest
state: running
register: container_ids
This task would store the list of running containers which use my-image:latest
to container_ids
. Then we exec the command:
- name: Exec the database update
cmd: "docker exec -it {{ item }} my-app-db-update.sh"
with: container_ids
Since we don't really want to use active containers for this type of operation, a better option would be to start a new single-use container acting on the same data instead:
- name: Run the database update
cmd: "docker run --rm --volumes-from {{ item }} --link:mydb:db my-app sh -c 'my-app-db-update.sh'"
with: container_ids
The above is only pseudo-code -- it won't actually run. How do I accomplish the task of storing a list of running docker containers which meet certain criteria, so that I can then apply a command to each container in the list using either docker exec
or docker run
?
There's surprisingly little about this online.