I am just trying to tell Ansible to build a docker image on my OS X machine and this is the error I get:
$ 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
This is the main.yml file I am using:
--- - hosts: localhost connection: local tasks: - name: Build docker image from dockerfiles docker_image: name: testimage path: test state: build
My Dockerfile:
# Build an example Docker container image. FROM busybox MAINTAINER Jeff Geerling <[email protected]> # Run a command when the container starts. CMD ["/bin/true”]”
My docker file is located in cookbook/test/Dockerfile
And the main.yml file is located in cookbook/main.yml
I'm running this on OS X.
I am totally lost at this point and any help would be very appreciated.
edit:
In response to Nathanial's request that I use -vvvv I get the following error:
(this is where the path is set to the subdirectory "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
In response to the above error, I tried giving the full path to the build directory where Dockerfile is located
path: "/Users/ronny/projects/dockers/tutorial/ansibledocker/test”
then I got:
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
edit #2
OK, I tried doing something different. instead of building an image, I tried to simply start one. After diggin in lib/python2.7/site-packages/ansible/modules/core/cloud/docker/docker.py I noticed that in class DockerManager.init around line 558, they set a few environment variables:
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')
So I spat out those values into a log file, and lo and behold, none of them were being set.
Then when I set them directly in 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
I was able to successfully start a container!
However, this method did not work with my initial issue, which is to build a docker image.
Digging further in docker_image.py I noticed that it breaks down around line 188 (I say "around" because I have logging breaks so I don't know the exact line) where it has the following code:
images = self.client.images()
so more digging and I see that self.client is checking out the docker_url at 'unix://var/run/docker.sock'
but over at this link I see that /var/run/docker.sock does not exist on OS X, instead one reply said that
/var/run/docker.sock will not be on your OSX filesystem - the Docker daemon is running inside the boot2docker VM - and that's where the unix socket is.
That serial file is also not related to the docker socket.
oyu need to talk to the TCP socket specified in the DOCKER_HOST env.
now I tried setting docker_url to the DOCKER_HOST URL, as the description for this modules says:
docker_url: description: - URL of docker host to issue commands to required: false default: unix://var/run/docker.sock aliases: []
but when I set it to the DOCKER_HOST address, I got an error.
Here is 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
and here is the error:
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
Any ideas?
As mentioned, you can use Ansible to automate Docker and to build and deploy Docker containers. First, you'll need to have Docker SDK for Python installed.
Using Ansible provides a simple, repeatable, defined state of your containers that can be easily tracked for any potential vulnerabilities, along with tracking actions like who deployed what container, with what code, where, and when.
Ansible has modules to manage the Docker server, Docker containers, and Docker Compose. These modules are maintained by the community. A dynamic inventory plugin for Docker exists. It retrieves the list of existing containers from Docker.
Defaults to unix://var/run/docker.sock . To connect to a remote host, provide the TCP connection string. For example: tcp://192.0.2.23:2376 . If TLS is used to encrypt the connection to the API, then the module will automatically replace 'tcp' in the connection URL with 'https'.
Your first configuration is almost "ok", two things need change
tasks: ... path: ./test state: present
the root directory to the ansible in your case is where your main.yml file is located.
cookbook/ ├── main.yml └── test └── Dockerfile
So the content of the main.yml stay this
--- - hosts: localhost connection: local tasks: - name: Build docker image from dockerfiles docker_image: name: testimage path: ./test state: present
The state needs to be present accord doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With