使用Ansible在OS X上运行docker任务的问题

时间:2015-05-30 23:20:18

标签: macos docker ansible

我只是想告诉Ansible在我的OS X机器上构建一个docker镜像,这是我得到的错误:

$ ansible-playbook main.yml

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

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [Build docker image from dockerfiles] ***********************************
failed: [localhost] => {"changed": false, "failed": true}
msg: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/ronny/main.retry

localhost                  : ok=1    changed=0    unreachable=0    failed=1

这是我正在使用的main.yml文件:

---
- hosts: localhost
  connection: local

  tasks:
        - name: Build docker image from dockerfiles
          docker_image:
            name: testimage
            path: test
            state: build

我的Dockerfile:

# Build an example Docker container image. 
FROM busybox 
MAINTAINER Jeff Geerling <geerlingguy@mac.com>

# Run a command when the container starts. 
CMD ["/bin/true”]”

我的泊坞窗文件位于 cookbook / test / Dockerfile

main.yml文件位于 cookbook / main.yml

我在OS X上运行它。

我完全迷失了,任何帮助都会非常感激。

修改

为了回应我使用-vvvv的Nathanials请求,我收到以下错误:

(这是路径设置到子目录&#34; test&#34;)

TASK: [Build docker image from dockerfiles] ***********************************
<localhost> ESTABLISH CONNECTION FOR USER: ronny
<localhost> REMOTE_MODULE docker_image name=test state=build path=test
<localhost> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/Users/ronny/.ansible/cp/ansible-ssh-%h-%p-%r" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 localhost /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724 && echo $HOME/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724'
<localhost> PUT /var/folders/s1/g6kswg952gvg5df6wld173480000gn/T/tmp3g0PIz TO /Users/ronny/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724/docker_image
<localhost> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/Users/ronny/.ansible/cp/ansible-ssh-%h-%p-%r" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 localhost /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /Users/ronny/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724/docker_image; rm -rf /Users/ronny/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724/ >/dev/null 2>&1'
failed: [localhost] => {"failed": true, "parsed": false}
Traceback (most recent call last):
  File "/Users/ronny/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724/docker_image", line 1852, in <module>
    main()
  File "/Users/ronny/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724/docker_image", line 235, in main
    image_id = manager.build()
  File "/Users/ronny/.ansible/tmp/ansible-tmp-1433079033.35-4833710313724/docker_image", line 140, in build
    stream = self.client.build(self.path, tag=':'.join([self.name, self.tag]), nocache=self.nocache, rm=True, stream=True)
  File "/usr/local/lib/python2.7/site-packages/docker/client.py", line 319, in build
    raise TypeError("You must specify a directory to build in path")
TypeError: You must specify a directory to build in path
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 78798
debug3: mux_client_request_session: session request sent
debug1: mux_client_request_session: master session id: 2
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 0
Shared connection to localhost closed.


FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/ronny/main.retry

localhost                  : ok=1    changed=0    unreachable=0    failed=1

为了响应上述错误,我尝试提供了Dockerfile所在的构建目录的完整路径

path: "/Users/ronny/projects/dockers/tutorial/ansibledocker/test”
然后我得到了:

TASK: [Build docker image from dockerfiles] ***********************************
<localhost> ESTABLISH CONNECTION FOR USER: ronny
<localhost> REMOTE_MODULE docker_image name=test state=build path=/Users/ronny/projects/dockers/tutorial/ansibledocker/test
<localhost> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/Users/ronny/.ansible/cp/ansible-ssh-%h-%p-%r" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 localhost /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1433079137.87-213359153110012 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1433079137.87-213359153110012 && echo $HOME/.ansible/tmp/ansible-tmp-1433079137.87-213359153110012'
<localhost> PUT /var/folders/s1/g6kswg952gvg5df6wld173480000gn/T/tmplH4Lln TO /Users/ronny/.ansible/tmp/ansible-tmp-1433079137.87-213359153110012/docker_image
<localhost> EXEC ssh -C -tt -vvv -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/Users/ronny/.ansible/cp/ansible-ssh-%h-%p-%r" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 localhost /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /Users/ronny/.ansible/tmp/ansible-tmp-1433079137.87-213359153110012/docker_image; rm -rf /Users/ronny/.ansible/tmp/ansible-tmp-1433079137.87-213359153110012/ >/dev/null 2>&1'
failed: [localhost] => {"changed": false, "failed": true}
msg: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/ronny/main.retry

localhost                  : ok=1    changed=0    unreachable=0    failed=1

编辑#2

好的,我尝试过不同的事情。而不是建立一个图像,我试图简单地开始一个。在lib / python2.7 / site-packages / ansible / modules / core / cloud / docker / docker.py中挖掘之后我注意到在第558行的DockerManager.init类中,他们设置了一些环境变量:

env_host = os.getenv('DOCKER_HOST')
env_docker_verify = os.getenv('DOCKER_TLS_VERIFY')
env_cert_path = os.getenv('DOCKER_CERT_PATH')
env_docker_hostname = os.getenv('DOCKER_TLS_HOSTNAME')

所以我将这些值吐出到日志文件中,并且看,没有一个被设置。

然后我直接在main.yml中设置它们:

- name: start container
      docker:
        name: mydata
        image: busybox
        state: present
      environment:
        DOCKER_HOST: tcp://192.168.59.103:2376
        DOCKER_TLS_VERIFY: 1
        DOCKER_CERT_PATH: /Users/ronny/.boot2docker/certs/boot2docker-vm
        DOCKER_TLS_HOSTNAME: boot2docker

我能够成功启动容器!

但是,这个方法不适用于我的初始问题,即构建一个泊坞窗图像。

在docker_image.py中进一步挖掘我注意到它在第188行周围发生故障(我说&#34;周围&#34;因为我有记录中断所以我不知道确切的行)它在哪里以下代码:

   images = self.client.images()

更多挖掘,我看到self.client正在'unix://var/run/docker.sock'

检查docker_url

但是在link处,我发现OS X上不存在/var/run/docker.sock,而是一个回复说

  

/var/run/docker.sock不会出现在您的OSX文件系统上 - Docker   守护进程在boot2docker VM中运行 - 那就是   unix socket是。

     

该串行文件也与docker socket无关。

     

oyu需要与DOCKER_HOST环境中指定的TCP套接字进行通信。

现在我尝试将docker_url设置为DOCKER_HOST URL,因为此模块的说明如下:

  

docker_url:       描述:          - 要发出命令的docker主机的URL       必需:false       默认值:unix://var/run/docker.sock       别名:[]

但是当我将它设置为DOCKER_HOST地址时,我收到了一个错误。

这是main.yml

   - name: Build docker image from dockerfiles 
          docker_image:
            name: testimage 
#            path: test
            path: /Users/ronny/projects/dockers/tutorial/ansibledocker/test
            state: build
            docker_url: 192.168.59.103:2376

这是错误:

failed: [localhost] => {"changed": false, "failed": true}
msg: ConnectionError(ProtocolError('Connection aborted.', BadStatusLine('\x15\x03\x01\x00\x02\x02\n',)),)

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/ronny/main.retry

localhost                  : ok=3    changed=0    unreachable=0    failed=1

有什么想法吗?

4 个答案:

答案 0 :(得分:0)

作为一种解决方法,您可以使用代理“socat-container”将您的请求从tcp地址路由到unix套接字。 看一下这个page。 boot2docker上的端口2376只适用于ipv6而不是ipv4(我发现没有办法改变这个,我没有深入研究这个问题而且不喜欢改变boot2docker VM的想法)。 我还在VirtualBox中为我的localhost(ansible inventory localhost)添加了NAT转换

小心使用ansible和docker-py

你需要:

  • ansible 1.9.2
  • docker_api_version:1.18

适合我。

答案 1 :(得分:0)

如果您想使用Ansible构建docker镜像,我建议您查看Ansible Container,它是Ansible的一个开源项目,专注于构建docker镜像。

Ansible Container使您可以使用现有/自己的Ansible角色来构建docker镜像。

答案 2 :(得分:0)

您的第一个配置几乎是“正常”,需要更改两件事

tasks: ...
  path: ./test
  state: present

在你的情况下,ansible的根目录是你的main.yml文件所在的位置。

cookbook/
├── main.yml
└── test
    └── Dockerfile

所以main.yml的内容保持不变

---
- hosts: localhost
  connection: local

  tasks:
  - name: Build docker image from dockerfiles
    docker_image:
      name: testimage
      path: ./test
      state: present

状态需要现在符合doc

答案 3 :(得分:-2)

如果在使用docker for mac(beta)时出现此错误,您可能忘记在远程服务器上安装docker。