我只是想让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”]”
我的 Docker 文件位于 cookbook/test/Dockerfile。
而 main.yml 文件位于 cookbook/main.yml。
我在 OS X 上运行此文件。
目前,我完全迷失了方向,希望能得到任何帮助。
编辑:
回应 Nathanial 的要求,我使用 -vvvv 后出现以下错误:
(这是路径设置为子目录 "test" 的地方)
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中的类DockerManager.init(大约在第558行左右)时,我注意到它们设置了一些环境变量:
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镜像。
在进一步研究docker_image.py时,我注意到它在大约第188行(我说“大约”是因为我有记录断点,所以我不知道确切的行数)处出现了以下代码:
images = self.client.images()
经过进一步了解,我发现self.client正在检查docker_url,其地址为'unix://var/run/docker.sock'
但在这个链接中,我发现在OS X上不存在/var/run/docker.sock文件,有一个回复说:
/var/run/docker.sock不会出现在你的OSX文件系统中 - Docker守护程序正在boot2docker VM内运行 - 这就是unix套接字所在的位置。
那个串行文件也与docker套接字无关。
您需要使用DOCKER_HOST环境中指定的TCP套接字进行通信。
现在我尝试将docker_url设置为DOCKER_HOST URL,因为此模块的描述如下:
docker_url: 描述: - 用于发布命令的docker主机的URL 必需:否 默认: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
有什么想法吗?
Dockerfile
的样例。 - BMWansible-playbook
命令时加上-vvv
参数,这样你就可以看到产生错误的实际命令。 - Nathaniel Waisbrot- hosts: localhost tasks: - name: remove image docker_image: name="my/app" state=absent
这在Linux上可以正常工作(应该在任何地方都可以),但在使用boot2docker的OSX上却不行。它会产生以下错误:ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)
。 - John Steel