gpt4 book ai didi

docker - 使用 Ansible 停止所有现有的 docker 容器

转载 作者:行者123 更新时间:2023-12-02 18:44:35 32 4
gpt4 key购买 nike

我正在创建一个 Ansible 角色来对托管不同 docker 容器的虚拟机进行操作系统和配置更新。
在角色开始时,我想停止所有 docker 容器(如果有的话)。我找到了 this thread ,但它有点旧,所以我试图打开一个新问题。希望没关系。
最简单的方法是:

- name: Stop docker containers
shell: |
docker stop $(docker ps -aq)
不幸的是,当主机没有 docker 容器时出现错误。并与 ignore_errors: yes 合作我认为这不是一个好方法。所以我尝试了这种方式
- 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
但仍与第一部分相同。当主机没有 docker 容器时出现错误。
因此,在链接的线程中,我尝试使用 docker_container像这样的模块:
- 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 }}'
不幸的是 docker_host_info模块不能正常工作,因为我所有的 docker 容器名称都以 / 开头.我已经为大家调试了:

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")"}


所以我的容器被命名为 /image-name而不是 image-name在 Ansible 为我创建的目录中。所以错误很明显,但我该如何解决呢?
也许这是一个模块问题,我需要去找 Ansible 开发人员?
谢谢并恭祝安康,

最佳答案

以下在我的家用机器上完美地完成了这项工作。如 docker_container module documentation 中所述,您可以使用其短或长 id 字符串作为 name 来识别正在运行的容器.长Id (大写 I )在 docker_host_info 的输出中可用在 containers列表。

---
- 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 }}"

演示运行:

# 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

关于docker - 使用 Ansible 停止所有现有的 docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61680945/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com