我有两个运行 Amazon Linux 2 操作系统的 ec2 实例 (amzn2-ami-hvm-2.0.20240610.1-x86_64-gp2)
uname -a
Linux ip-172-31-24-106.eu-central-1.compute.internal 4.14.345-262.561.amzn2.x86_64 #1 SMP Fri May 31 18:15:42 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
在其中一个上,我手动安装 docker 和 docker-compose,如下所示:
# # # Install docker # # #
sudo yum update -y
sudo yum install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker version
Client:
Version: 20.10.25
# # # Install docker-compose # # #
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version
Docker Compose version v2.27.1
ls -l /usr/local/bin/docker-compose
-rwxr-xr-x 1 root root 63052874 Jun 17 16:54 /usr/local/bin/docker-compose
这样一切就正常了。
但是,当我尝试通过 Ansible 安装 docker 和 docker-compose 时,出现错误。 这是我应用的剧本:
---
- name: Install python3, docker, docker-compose
hosts: [docker_server]
become: yes
gather_facts: False
tasks:
- name: Install python3 and docker
ansible.builtin.yum:
use_backend: yum3
name:
- docker
- python3-pip
update_cache: yes
state: present
- name: Install Docker Compose
get_url:
url: https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64
dest: /usr/local/bin/docker-compose
mode: '+x'
register: docker_compose_result
- debug: var=docker_compose_result
- name: Start docker daemon
ansible.builtin.systemd:
name: docker
state: started
- name: Install docker python module
ansible.builtin.pip:
name:
- docker
- docker-compose
- name: Add ec2-user to docker group
hosts: [docker_server]
become: yes
tasks:
- name: Add ec2-user to docker group
ansible.builtin.user:
name: ec2-user
groups: docker
append: yes
- name: Reconnect to server session
ansible.builtin.meta: reset_connection
- name: Start docker containers
hosts: [docker_server]
vars_files:
- project-vars
tasks:
- name: Copy docker compose
ansible.builtin.copy:
src: /home/tomas/gitlab/ansible-learn-nana/docker-compose.yaml
dest: /home/ec2-user/docker-compose.yaml
- name: Start container from compose
community.docker.docker_compose_v2:
project_src: /home/ec2-user
这是我得到的错误:
PLAY [Install python3, docker, docker-compose] ***********************************************************************************************************************************************
TASK [Install python3 and docker] ************************************************************************************************************************************************************
changed: [18.153.66.78]
TASK [Install Docker Compose] ****************************************************************************************************************************************************************
changed: [18.153.66.78]
TASK [debug] *********************************************************************************************************************************************************************************
ok: [18.153.66.78] => {
"docker_compose_result": {
"changed": true,
"checksum_dest": null,
"checksum_src": "38ab1e3229c2627dd788d299e526145491aadd1d",
"dest": "/usr/local/bin/docker-compose",
"elapsed": 1,
"failed": false,
"gid": 0,
"group": "root",
"md5sum": "24120814a7df4f78aca2a31b17067e64",
"mode": "0755",
"msg": "OK (63052874 bytes)",
"owner": "root",
"size": 63052874,
"src": "/home/ec2-user/.ansible/tmp/ansible-tmp-1718643910.4494328-246157-215366873451751/tmp4m7jlk7a",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64"
}
}
TASK [Start docker daemon] *******************************************************************************************************************************************************************
changed: [18.153.66.78]
TASK [Install docker python module] **********************************************************************************************************************************************************
changed: [18.153.66.78]
PLAY [Add ec2-user to docker group] **********************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [18.153.66.78]
TASK [Add ec2-user to docker group] **********************************************************************************************************************************************************
changed: [18.153.66.78]
TASK [Reconnect to server session] ***********************************************************************************************************************************************************
PLAY [Start docker containers] ***************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [18.153.66.78]
TASK [Copy docker compose] *******************************************************************************************************************************************************************
changed: [18.153.66.78]
TASK [Start container from compose] **********************************************************************************************************************************************************
fatal: [18.153.66.78]: FAILED! => {"changed": false, "msg": "Docker CLI /usr/bin/docker does not have the compose plugin installed"}
PLAY RECAP ***********************************************************************************************************************************************************************************
18.153.66.78 : ok=9 changed=6 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
当我
ssh
进入这台应该由Ansible配置的机器时,这就是我所看到的:
cd /usr/local/bin/ # Directory where curl downloads docker-compose
ls -l
total 24
-rwxr-xr-x 1 root root 212 Jun 17 17:05 distro
-rwxr-xr-x 1 root root 215 Jun 17 17:05 docker-compose
-rwxr-xr-x 1 root root 212 Jun 17 17:05 dotenv
-rwxr-xr-x 1 root root 213 Jun 17 17:05 jsonschema
-rwxr-xr-x 1 root root 233 Jun 17 17:05 normalizer
-rwxr-xr-x 1 root root 216 Jun 17 17:05 wsdump
如果我尝试在这台失败的机器上手动下载 docker-compose,文件大小会有所不同:
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-compose
cd /usr/local/bin/ # Directory where curl downloads docker-compose
ls -l
total 61596
-rwxr-xr-x 1 root root 212 Jun 17 17:05 distro
-rwxr-xr-x 1 root root 63052874 Jun 17 17:08 docker-compose
-rwxr-xr-x 1 root root 212 Jun 17 17:05 dotenv
-rwxr-xr-x 1 root root 213 Jun 17 17:05 jsonschema
-rwxr-xr-x 1 root root 233 Jun 17 17:05 normalizer
-rwxr-xr-x 1 root root 216 Jun 17 17:05 wsdump
所以看来我的playbook在这项任务上失败了:
- name: Install Docker Compose
get_url:
url: https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64
dest: /usr/local/bin/docker-compose
mode: '+x'
register: docker_compose_result
- debug: var=docker_compose_result
即使调试显示
"status_code": 200
任何关于我可以在哪里查看的建议都将受到高度赞赏。
希望您度过愉快的一天。我遇到了同样的问题。
问题是您正在将community.docker.docker_compose_v2与docker compose版本1一起使用。 我想说的是,我不建议您返回community.docker.docker_compose,因为它已被弃用。
我在 Amazon Linux 上也面临同样的问题,我可以通过两种方式解决。不理想,但这是暂时的:
第一个解决方法:
- name: Start containers from compose
ansible.builtin.command:
cmd: docker-compose up -d
第二个解决方法:
become: yes
vars_files:
- project-vars
tasks:
- name: Copy Docker compose
copy:
src: /root/ansible/Ansible/docker-compose.yaml
dest: /home/ec2-user
- name: Docker login
docker_login:
registry_url: https://index.docker.io/v1
username: ibrahimosama
password: "{{docker_password}}"
- name: Start containers from compose
community.docker.docker_compose_v2:
project_src: /home/ec2-user
最终的解决方法是安装 docker compose 版本 2:
- name: Install docker compose V2
hosts: ec2
become: yes
tasks:
- name: Create directory for Docker CLI plugins
file:
path: /usr/local/lib/docker/cli-plugins
state: directory
mode: '0755'
- name: Download Docker Compose V2 binary
get_url:
url: https://github.com/docker/compose/releases/download/v2.18.0/docker-compose-linux-x86_64
dest: /usr/local/lib/docker/cli-plugins/docker-compose
mode: '0755'
- name: Make Docker Compose binary executable
file:
path: /usr/local/lib/docker/cli-plugins/docker-compose
mode: '0755'
- name: Verify Docker Compose installation
command: docker compose version
register: docker_compose_version
ignore_errors: yes
- name: Show Docker Compose version
debug:
msg: "{{ docker_compose_version.stdout }}"
when: docker_compose_version.rc == 0
我的完整项目位于 https://github.com/ibrahim-osama-amin/Ansible/blob/main/deploy-docker.yaml