# Ansible's docker module volumes option is a mystery (to me)

**URL:** <https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044>\
**Category:** Ansible Project\
**Created:** [August 26, 2014, 7:05pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044 "2014-08-26T19:05:49Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Truong](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@Steven\_Truong](https://forum.ansible.com/u/Steven_Truong)\
**Post date:** [August 26, 2014, 7:05pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/1 "2014-08-26T19:05:49Z")

</div>

Hi all,

I tried to use the volumes option and nothing seems to work for me.

I want to mount 2 directories ~/dockerstorage/model and ~/repo/ops/ansible to the container and I used either of these commands and nothing worked.

docker: [image:5000=registry.my.com/steven/centos65\_prod\_ready:v1](http://image:5000=registry.my.com/steven/centos65_prod_ready:v1) docker\_url=tcp://{{ ansible\_default\_ipv4.address }}:7777 publish\_all\_ports=True state=running volumes=/home/kafka/models:{{ myhome.stdout }}/dockerstorage/models:rw,/usr/local/src/ansible:{{ myhome.stdout }}/repo/ops/ansible:ro

docker: [image:5000=registry.my.com/steven/centos65\_prod\_ready:v1](http://image:5000=registry.my.com/steven/centos65_prod_ready:v1) docker\_url=tcp://{{ ansible\_default\_ipv4.address }}:7777 publish\_all\_ports=True state=running volumes={{ myhome.stdout }}/dockerstorage/models:/home/kafka/models:rw,{{ myhome.stdout }}/repo/ops/ansible:/usr/local/src/ansible:ro

I sshed into the containers and the mounted points in either case were all empty.

I ran using docker directly and it worked for me:

```auto

```

```auto

```

```auto
docker run -d -P -v /home/steven/dockerstorage/models:/home/kafka/models:rw -v /home/steven/repo/ops/ansible:/usr/local/src/ansible:ro registry.my.com:5000/steven/centos65_prod_ready:v1

```

```auto

```

```auto

```

Please let me know what is the right ways to use “volumes” for docker. \>From the comments in the docker module, it appears to me that the /mnt:/tmp is in reversed orders from those of the docker command line because of the case when we just want to create a volume such as /mnt and there is no equivalent mounted point from the host. I think that this is confusing and users have to read the codes to find out.

Thanks,  
Steven.

[root@sc2-dock1 cloud]# pwd  
/usr/local/ansible/library/cloud

class DockerManager:

counters = {‘created’:0, ‘started’:0, ‘stopped’:0, ‘killed’:0, ‘removed’:0, ‘restarted’:0, ‘pull’:0}

def **init** (self, module):  
self.module = module

self.binds = None  
self.volumes = None  
if self.module.params.get(‘volumes’):  
self.binds = {}  
self.volumes = {}  
vols = self.module.params.get(‘volumes’)  
for vol in vols:  
parts = vol.split(“:”)

# host mount (e.g. /mnt:/tmp, bind mounts host’s /tmp to /mnt in the container)

if len(parts) == 2:  
self.volumes[parts[1]] = {}  
self.binds[parts[0]] = parts[1]

# docker mount (e.g. /www, mounts a docker volume /www on the container at the same location)

else:  
self.volumes[parts[0]] = {}

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [August 26, 2014, 7:16pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/2 "2014-08-26T19:16:18Z")

</div>

If you remove the variables and hard-code the paths, does it work for you then? I’m curious if things like spaces/newlines in the stdout variables you’re using are causing the problem.

You might want to put in a “- debug: var=myhome” before the docker task to view the value of stdout there.

---

<div class="post-metadata">

**Author:** ![Steven\_Truong](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@Steven\_Truong](https://forum.ansible.com/u/Steven_Truong)\
**Post date:** [August 26, 2014, 7:26pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/3 "2014-08-26T19:26:57Z")

</div>

TASK: [echo $myhome] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“myhome”: {  
“changed”: true,  
“cmd”: “echo $HOME”,  
“delta”: “0:00:00.165168”,  
“end”: “2014-08-26 19:22:36.055947”,  
“invocation”: {  
“module\_args”: “echo $HOME”,  
“module\_name”: “shell”  
},  
“rc”: 0,  
“start”: “2014-08-26 19:22:35.890779”,  
“stderr”: “”,  
“stdout”: “/root”,  
“stdout\_lines”: [  
“/root”  
]  
}  
}

I removed :rw and :ro and it still did not work. I think that Ansible does not support these third fields yet.

Steven.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [August 27, 2014, 12:27am UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/4 "2014-08-27T00:27:44Z")

</div>

I merged in support for specifying :ro/:rw a few weeks ago, and it was included in the 1.7.1 release.

---

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [September 8, 2014, 4:09pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/5 "2014-09-08T16:09:03Z")

</div>

Volumes also don’t appear to be working for me either. I can start the container, but the mounted volume is always empty inside the container, despite the host having two volumes. As the original poster says, if I just use the docker command directly, the volume mount works as expected.

- name: Start Registry  
docker: image={{docker\_image}} state=running volumes=“/etc/docker-registry/keys:/etc/docker-registry/keys” name=registry

See anything wrong?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 8, 2014, 9:41pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/6 "2014-09-08T21:41:14Z")

</div>

Can you share the version of Ansible, docker-py, and Docker being used in this case?

Thanks!

---

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [September 8, 2014, 9:44pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/7 "2014-09-08T21:44:50Z")

</div>

[clduser@docker-registry ~]$ sudo docker -v  
Docker version 1.1.2, build d84a070/1.1.2

[clduser@docker-registry ~]$ pip list | grep docker  
docker-py (0.4.0)

Using the latest ‘devel’ branch of Ansible. Just repulled now and still experiencing this as of commit c610783f900586b170a6dfa3a02696f568a11728.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 9, 2014, 4:43pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/8 "2014-09-09T16:43:34Z")

</div>

Ok can you please file a ticket on this one so we can investigate?

Thanks!

---

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [September 12, 2014, 1:08pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/9 "2014-09-12T13:08:08Z")

</div>

So I actually got this working. It think some of my confusion comes from how to specify ‘list’ variables. The docker module, says the ‘volumes’ parameter was a ‘list’. Being new to Ansible and with no volumes example, I dug around a bit. EC2 module has a ‘list’ var of instance-ids and this example:

`  
vars:  
instance\_ids:

- ‘i-xxxxxx’
- ‘i-xxxxxx’
- ‘i-xxxxxx’  
region: us-east-1  
tasks:
- name: Start the sandbox instances  
local\_action:  
module: ec2  
instance\_ids: ‘{{ instance\_ids }}’  
`

So I tried to copy that with docker:

`  
vars:  
theVolumes:

- ‘/etc/foo:/foo’
- ‘/etc/bar:/bar’  
tasks:
- name: run bash  
docker:  
image=centos  
volumes=‘{{ theVolumes }}’  
state=running  
stdin\_open=yes tty=yes  
command=/bin/bash  
`

But docker client does not like that.

> \<96.119.0.167\> REMOTE\_MODULE docker image=centos volumes=‘[’/etc/foo:/foo’, ‘/etc/bar:/boo’]’ state=running stdin\_open=yes tty=yes command=/bin/bash  
> msg: Docker API error: Cannot start container 35fb847fdc84ff5bcbdce19b9a443a4eb86de5018d96fd620ca22dcb7dc4f3b0: /etc/bar must be an absolute path

Despite the fact that it appears to have parse the volumes correctly, I get the absolute path error seen above. If I change the command to:

`  
tasks:

- name: run bash  
docker:  
image=centos  
volumes=‘/etc/foo:foo,/etc/bar:bar’  
state=running  
stdin\_open=yes tty=yes  
command=/bin/bash

`

My guess is that this works with ec2 because that’s running locally, but docker has to serialize the list to the server? I think my other issues were red-herrings and I was getting confused reading the code. How does the docker module know that a string with commas in it is a list?

`  
if self.module.params.get(‘volumes’):  
self.binds = {}  
self.volumes = {}  
vols = self.module.params.get(‘volumes’)  
for vol in vols:

`

Is the fact that it is annotated a ‘list’ in the module spec (volumes= dict(default=None, type=‘list’)) make Ansible ‘pre-parse’ that String into a List? Should the user really have to change how they specify a list based on whether it is executed locally or remotely? I am able to use var if I do this:

`  
vars:  
theVolumes:

- ‘/etc/foo:/foo’
- ‘/etc/bar:/bar’  
tasks:
- name: run bash  
docker:  
image=centos  
volumes={{ theVolumes | join(‘,’) }}  
state=running  
stdin\_open=yes tty=yes  
command=/bin/bash

`

But IMO if their is Ansible magic with lists going on, it should do that already.

---

<div class="post-metadata">

**Author:** ![Bk\_Lau](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bk\_Lau](https://forum.ansible.com/u/Bk_Lau)\
**Post date:** [September 26, 2014, 6:56pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/10 "2014-09-26T18:56:53Z")

</div>

Any final prognosis on this issue? Specfically, how should the volumes be specified. An example would be nice!  
I’m urgently waiting to use this feature.  
–BK

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 26, 2014, 7:41pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/11 "2014-09-26T19:41:31Z")

</div>

@Matt - the issue you’re seeing is due to the fact that your missing YAML lists with the k=v syntax for modules. If you used the “complex arguments” syntax for the module, you wouldn’t have to do the join. For example:

vars:  
theVolumes:

- ‘/etc/foo:/foo’
- ‘/etc/bar:/bar’  
tasks:
- name: run bash  
docker:  
image: centos  
volumes: “{{ theVolumes }}”  
state: running  
stdin\_open: yes  
tty: yes  
command: /bin/bash

should work without any issues.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 27, 2014, 1:39pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/12 "2014-09-27T13:39:20Z")

</div>

The user does not have to change how they specify a list based on it being local or remote - that is not a thing in Ansible.

You do need to use the “:” form in the arguments though, any time you want to pass structured data to a module.

---

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [September 29, 2014, 1:25pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/13 "2014-09-29T13:25:09Z")

</div>

Ah, so = does an implicit ‘toString’ on the variable I presume?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 29, 2014, 1:42pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/14 "2014-09-29T13:42:35Z")

</div>

Yeah basically - it’s Jinja2 templated down to a string, losing any type info.

---

<div class="post-metadata">

**Author:** ![Lokesh\_Dokara](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@Lokesh\_Dokara](https://forum.ansible.com/u/Lokesh_Dokara)\
**Post date:** [December 22, 2014, 4:03pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/15 "2014-12-22T16:03:56Z")

</div>

Hi all,

Docker volumes worked fine for me as long as I use ansible to run docker on local system, but not working fine when I try to use ansible to run docker on a remote node. My question is whether docker expects the host directory in volumes option to be on system running ansible or the remote node. I’ve got no results when I had host directory in remote node.

---

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [December 23, 2014, 7:04pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/16 "2014-12-23T19:04:08Z")

</div>

The volumes need to be where docker is running. So yes, host directory should be on the remote node running docker.

---

<div class="post-metadata">

**Author:** ![Lokesh\_Dokara](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@Lokesh\_Dokara](https://forum.ansible.com/u/Lokesh_Dokara)\
**Post date:** [December 26, 2014, 11:13am UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/17 "2014-12-26T11:13:32Z")

</div>

But ansible module is not working properly on remote nodes

---

<div class="post-metadata">

**Author:** ![Matt\_Hughes](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Matt\_Hughes](https://forum.ansible.com/u/Matt_Hughes)\
**Post date:** [December 26, 2014, 5:34pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/18 "2014-12-26T17:34:48Z")

</div>

Do you have the required Python module installed on the remote node? I believe it is docker-py but check the docket module to be sure.

---

<div class="post-metadata">

**Author:** ![Lokesh\_Dokara](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@Lokesh\_Dokara](https://forum.ansible.com/u/Lokesh_Dokara)\
**Post date:** [December 26, 2014, 6:03pm UTC](https://forum.ansible.com/t/ansibles-docker-module-volumes-option-is-a-mystery-to-me/16044/19 "2014-12-26T18:03:39Z")

</div>

Yeah latest version (0.7.0) of docker-py is installed. Except volumes option all other docker options are working fine.
