Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping all existing docker containers with ansible

Tags:

docker

ansible

I'm creating an Ansible role to do a OS and config update on VMs on which I'm hosting different docker containers.

At beginning of the role I want to stop all docker containers if there are some. I've found this thread, but it is a bit old so I'm trying to open a new question. Hope that's ok.

The simplest way would be this:

- name: Stop docker containers
  shell: |
    docker stop $(docker ps -aq)

Unfortunately I'm getting an error when a host has no docker container. And working with ignore_errors: yes wouldn't be a good way I think. So I tried that way

- name: Get info on docker host and list images
  docker_host_info:
    containers: yes
  register: containers_to_stop

- name: Stop docker containers
  shell: |
    docker stop $(docker ps -aq)
  when: containers_to_stop.containers != 0

but still the same as in first part. I'm getting an error when a host has no docker container.

So as in the linked thread I'm trying to use the docker_container module like this:

- name: Get info on docker host and list images
  docker_host_info:
    containers: yes
  register: containers_to_stop

- name: Stop running docker containers
  docker_container:
    name: '{{ item.Names }}'
    image: '{{ item.Image }}'
    state: stopped
  loop: '{{ containers_to_stop.containers }}'

Unfortunately the docker_host_info module doesn't work fine because all my docker container names will begin with a /. I've debugged that for you all:

failed: [app01] (item={u'Status': u'Up 12 minutes', u'Command': u'./replace_props_and_start.sh', u'Names': [u'/image-name'], u'Created': 1588071879, u'Image': u'image-name', u'Ports': [{u'IP': u'0.0.0.0', u'Type': u'tcp', u'PublicPort': 8091, u'PrivatePort': 80}], u'Id': u'ad5b0b3d6d623e2ac1d0a2ead9fbbf8a5ce5bca58492410a31035fd160de149a'}) => {"ansible_loop_var": "item", "changed": false, "item": {"Command": "./replace_props_and_start.sh", "Created": 1588071879, "Id": "ad5b0b3d6d623e2ac1d0a2ead9fbbf8a5ce5bca58492410a31035fd160de149a", "Image": "image-name", "Names": ["/image-name"], "Ports": [{"IP": "0.0.0.0", "PrivatePort": 80, "PublicPort": 8091, "Type": "tcp"}], "Status": "Up 12 minutes"}, "msg": "Error creating container: 400 Client Error: Bad Request ("Invalid container name (['/image-name']), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed")"}

So my container is named /image-name and not image-name in the directory which Ansible is creating for me. So the error is clear, but how could I fix that?

Maybe that's a module problem and I need to go to Ansible developers?

Thanks and regards,

like image 215
gurbelunder Avatar asked Dec 17 '22 13:12

gurbelunder


2 Answers

The following is doing the job perfectly fine on my home machine. As stated in the docker_container module documentation, you can identify a running container using its short or long id string as name. The long Id (with a capital I) is available in the output of docker_host_info in the containers list.

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: Get running containers
      docker_host_info:
        containers: yes
      register: docker_info

    - name: Stop running containers
      docker_container:
        name: "{{ item }}"
        state: stopped
      loop: "{{ docker_info.containers | map(attribute='Id') | list }}"

Demo run:

# Show we have no running containers
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# Spawn some test containers for demo
$ for i in $(seq 1 5); do docker run -d --rm centos:8 bash -c "while true; do sleep 1; done"; done
a492efab9ec7dace786b610f3b93c335fbb84f041f7954557e971a5cbb0905a0
8cd55145c7cb267b37d2af346571797e283cac75777c531caeb88df7ec2e57d6
f009140260f5daee6efc6fba8dd8f73f9c83e31e7f1e09d48681b0738bc86f50
e7af30b1ade41fbc65b3db8e4146497ee736065103af769331d9df4e8e39b131
643e6831b958e0410bb148aeaec29dfeec6fa2773af5fb286ad74ab0368f2e50

# Make sure containers are running
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
643e6831b958        centos:8            "bash -c 'while true…"   9 seconds ago       Up 7 seconds                            quizzical_allen
e7af30b1ade4        centos:8            "bash -c 'while true…"   10 seconds ago      Up 9 seconds                            frosty_khayyam
f009140260f5        centos:8            "bash -c 'while true…"   12 seconds ago      Up 10 seconds                           ecstatic_ramanujan
8cd55145c7cb        centos:8            "bash -c 'while true…"   14 seconds ago      Up 12 seconds                           focused_sammet
a492efab9ec7        centos:8            "bash -c 'while true…"   15 seconds ago      Up 13 seconds                           agitated_jones

# Stop containers with playbook
$ ansible-playbook test.yml 

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Get running containers] *************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Stop running containers] ************************************************************************************************************************************************************************************
changed: [localhost] => (item=643e6831b958e0410bb148aeaec29dfeec6fa2773af5fb286ad74ab0368f2e50)
changed: [localhost] => (item=e7af30b1ade41fbc65b3db8e4146497ee736065103af769331d9df4e8e39b131)
changed: [localhost] => (item=f009140260f5daee6efc6fba8dd8f73f9c83e31e7f1e09d48681b0738bc86f50)
changed: [localhost] => (item=8cd55145c7cb267b37d2af346571797e283cac75777c531caeb88df7ec2e57d6)
changed: [localhost] => (item=a492efab9ec7dace786b610f3b93c335fbb84f041f7954557e971a5cbb0905a0)

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# Verify containers are stopped
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# Check that playbook succeeds without containers running
$ ansible-playbook test.yml 

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Get running containers] *************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Stop running containers] ************************************************************************************************************************************************************************************

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
like image 95
Zeitounator Avatar answered Jan 09 '23 04:01

Zeitounator


Unfortunately the "docker_host_info" Module doesn't work fine because all my docker container names will begin with a "/".

That's not the (entire) problem; the issue is that in the value returned by docker_host_info, names is a list. Take a closer look at that error message:

400 Client Error: Bad Request (\"Invalid container name (['/image-name']), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed\")"}

See how you're passing ['/image-name'] for the container name? You would need to do something like this:

- name: Get info on docker host and list images
  docker_host_info:
    containers: yes
  register: containers_to_stop

- name: Stop running docker containers
  docker_container:
    name: '{{ item.Names.0[1:] }}'
    image: '{{ item.Image }}'
    state: stopped
  loop: '{{ containers_to_stop.containers }}'

In the above code, we're asking for the first element in the Names list (item.Names.0), and then we're selecting everything but the first character (so /something becomes something).

like image 22
larsks Avatar answered Jan 09 '23 05:01

larsks