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.
Best Answer
Given the following example output from
docker ps
:This playbook will give you an idea of how to capture the data you need and how to run actions iterating on the provided list. It's a very simple example, and you'll have to adapt it to your needs. That's on purpose:
The interesting parts are:
Gather the list of containers in a given host (
localhost
in my example). I've used a plainshell
invocation to be able to useawk
to filter out the output. The result is stored in a register. Since the input is a list, this will have a direct consequence on how to retrieve the data back, more below. Uncomment thedebug
task in between to compare the data stored in the register with and without a list.Iterate over the register's results (container ID) and use the
docker_container
module to run an action (command
parameter). You can uselinks
andvolumes_from
in yourdocker_container
invocation. Check the on-line documentation of the module for the details.