docker 连接时出错:获取服务器API版本时出错:Ansible

lf3rwulv  于 2023-10-16  发布在  Docker
关注(0)|答案(2)|浏览(131)

我是Ansible的新手。我已经运行了以下ansible PlayBook,发现了这些错误:

---
- hosts: webservers
  remote_user: linx
  become: yes
  become_method: sudo
  tasks:

    - name: install docker-py
      pip: name=docker-py

    - name: Build Docker image from Dockerfile
      docker_image:
        name: web
        path: docker
        state: build

    - name: Running the container
      docker_container:
        image: web:latest
        path: docker
        state: running

    - name: Check if container is running
      shell: docker ps

错误消息:
失败!=> {“changed”:false,“msg”:“连接错误:获取服务器API版本时出错:('连接中止',error(2,'没有这样的文件或目录')")
这是我的文件夹结构:

.
├── ansible.cfg
├── docker
│   └── Dockerfile
├── hosts
├── main.retry
├── main.yml

我很困惑,docker文件夹已经在我的本地,但不知道为什么我遇到了这些错误信息.

rwqw0loc

rwqw0loc1#

我发现解决方案是Ansible安装DockerDocker守护程序不工作。在我的游戏板中需要添加以下命令。

---
- hosts: webservers
  remote_user: ec2-user
  become: yes
  become_method: sudo
  tasks:
    - name: install docker
      yum: name=docker

    **- name: Ensure service is enabled
      command: service docker restart***

    - name: copying file to remote
      copy:
        src: ./docker
        dest: /home/ec2-user/docker
    - name: Build Docker image from Dockerfile
      docker_image:
        name: web
        path: /home/ec2-user/docker
        state: build
    - name: Running the container
      docker_container:
        image: web:latest
        name: web
    - name: Check if container is running
      shell: docker ps
zdwk9cvp

zdwk9cvp2#

我也遇到过同样的问题。我尝试执行docker login,得到同样奇怪的错误。在我的例子中,ansible用户没有必要的docker凭据。在这种情况下,解决方案是切换到具有docker凭据的用户:

- name: docker login
  hosts: my_server
  become: yes
  become_user: docker_user
  tasks:
    - docker_login:
        registry: myregistry.com
        username: myusername
        password: mysecret

相关问题