# connect to Docker container on remote host

**URL:** <https://forum.ansible.com/t/connect-to-docker-container-on-remote-host/19848>\
**Category:** Ansible Project\
**Created:** [November 25, 2015, 2:25am UTC](https://forum.ansible.com/t/connect-to-docker-container-on-remote-host/19848 "2015-11-25T02:25:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Steinbach](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@Thomas\_Steinbach](https://forum.ansible.com/u/Thomas_Steinbach)\
**Post date:** [November 25, 2015, 2:25am UTC](https://forum.ansible.com/t/connect-to-docker-container-on-remote-host/19848/1 "2015-11-25T02:25:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 25, 2015, 2:40am UTC](https://forum.ansible.com/t/connect-to-docker-container-on-remote-host/19848/2 "2015-11-25T02:40:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Thomas\_Steinbach](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@Thomas\_Steinbach](https://forum.ansible.com/u/Thomas_Steinbach)\
**Post date:** [November 25, 2015, 3:08am UTC](https://forum.ansible.com/t/connect-to-docker-container-on-remote-host/19848/3 "2015-11-25T03:08:15Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 25, 2015, 7:12am UTC](https://forum.ansible.com/t/connect-to-docker-container-on-remote-host/19848/4 "2015-11-25T07:12:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Thomas\_Steinbach](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@Thomas\_Steinbach](https://forum.ansible.com/u/Thomas_Steinbach)\
**Post date:** [November 29, 2015, 8:03pm UTC](https://forum.ansible.com/t/connect-to-docker-container-on-remote-host/19848/5 "2015-11-29T20:03:50Z")

</div>

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)?
