connect to Docker container on remote host

572 views
Skip to first unread message

Thomas Steinbach

unread,
Nov 24, 2015, 9:25:29 PM11/24/15
to Ansible Project
Hi,

since Ansible 2.0 it is possible to deploy directly to a docker container via connection=docker. Now I want to create a Role which is starting a Docker container and deploying some stuff to it. Here is an arbitrary example of my current solution:

- name: create docker container
  docker:
    name: mycontainer
    image: my/container
    ports:
      - "2222:22"
    state: running

- name: add aci_jenkins to inventory
  add_host:
    name: mycontainer
    ansible_connection: docker

- name: copy jenkins private key
  delegate_to: mycontainer
  copy:
    content: "{{ ID_RSA }}"
    dest: /home/user/.ssh/id_rsa
    mode: 0700

This approach works quite fine since I deploy the role on my localhost. Because the deletage_to directive is delegating from the ansible machine (my localhost) it will find the docker container started. If I deploy the Role on a remote host, the container will be started on the remote host, but the delegate_to directive will looking for the container on the local host. Currently there is no documentation of the docker connection. Does anybody know how I redirect the docker connection to the remote host I started the container in?

Brian Coca

unread,
Nov 24, 2015, 9:40:25 PM11/24/15
to Ansible Project
the docker connection plugin will only connect to hosts on the ansible
control machine, it is using the local execution tools to access the
docker instance w/o using ssh.

--
Brian Coca

Thomas Steinbach

unread,
Nov 24, 2015, 10:08:16 PM11/24/15
to Ansible Project
Thank you for quick reply. Is it possible to pass the -H / --host parameter to the local docker binary, such that you can specify the socket to talk to the (remote) Docker daemon (e.g. tcp://myhost:port/path)?

Brian Coca

unread,
Nov 25, 2015, 2:12:43 AM11/25/15
to Ansible Project
The current docker connection plugin does not seem to support that
(yet). I suggest opening a github feature request to allow for usage
with the docker daemon.

--
Brian Coca

Thomas Steinbach

unread,
Nov 29, 2015, 3:03:50 PM11/29/15
to Ansible Project
Is there a possibility to define the command docker is called with? This would solve my problem, since I have created an alias calling the remote Docker daemon. Within the connection/docker.py there is following code:

    def __init__(self, play_context, new_stdin, *args, **kwargs):
        super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)

        ...

        if 'docker_command' in kwargs:
            self.docker_cmd = kwargs['docker_command']

How can I set the 'docker_command' from **kwargs within my Ansible Role (or Playbook)?
Reply all
Reply to author
Forward
0 new messages