# Ansible docker\_container task cannot start a container while the equivalent docker run command can

**URL:** <https://forum.ansible.com/t/ansible-docker-container-task-cannot-start-a-container-while-the-equivalent-docker-run-command-can/24798>\
**Category:** Ansible Project\
**Created:** [June 22, 2017, 9:55am UTC](https://forum.ansible.com/t/ansible-docker-container-task-cannot-start-a-container-while-the-equivalent-docker-run-command-can/24798 "2017-06-22T09:55:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [June 22, 2017, 9:55am UTC](https://forum.ansible.com/t/ansible-docker-container-task-cannot-start-a-container-while-the-equivalent-docker-run-command-can/24798/1 "2017-06-22T09:55:35Z")

</div>

I am using docker\_container module on my infra quite extensively for some time and now i am stuck at a problem for which i cannot figure out the reason. One point to note here is that for the following chances, there is lesses probability of messing up somewhere else, like in the whole script, inventory or with the Ansible host.

I was trying to start a basic container with an ansible script with the following task:

`

- name: start deploymentService containers  
docker\_container:  
name: name  
image: image  
state: started  
restart\_policy: “unless-stopped”  
volumes:
- /this\_is\_a\_dir:/opt/existing\_dir\_in\_image:Z  
`

The task runs fine, but the container exits after a few seconds always. In this case it will be stuck in some sort of restart loop because of the restart policy. The image i am using here is pretty basic and i am using this image to manually run containers for a while without any problem. By its design, there are not logs generated so docker logs gives me nothing.

I tried to replicate the docker command equivalent to the above task:  
Note that i have changed the names and paths only but on my system i used the exact same syntax, nothing missing in this post.

`docker run -itd --name=name --restart="unless-stopped" -v /this_is_a_dir:/opt/existing_dir_in_image:Z image`

This command works fine when i run this on the host, the container doesn’t crash. When i use the same command in my playbook with the command module, it again works fine proving that there is no problem elsewhere in the playbook or inventory. I am using Ansible 2.2 and tried this on 2.3 as well, but absolutely no luck.  
Can someone tell what the problem is here ?

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [June 24, 2017, 4:32pm UTC](https://forum.ansible.com/t/ansible-docker-container-task-cannot-start-a-container-while-the-equivalent-docker-run-command-can/24798/2 "2017-06-24T16:32:53Z")

</div>

Hi,

> I tried to replicate the docker command equivalent to the above task:  
> Note that i have changed the names and paths only but on my system i  
> used the exact same syntax, nothing missing in this post.
> 
> docker run -itd --name=name --restart="unless-stopped"  
> -v /this\_is\_a\_dir: /opt/existing\_dir\_in\_image:Z image

here you specify "-i" (Keep STDIN open even if not attached) and "-t"  
(Allocate a pseudo-TTY), while in the ansible task:

> - name: start deploymentService containers  
> &nbsp;&nbsp;docker\_container:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image: image  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: started  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restart\_policy: "unless-stopped"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;volumes:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /this\_is\_a\_dir:/opt/existing\_dir\_in\_image:Z

you specify neither "interactive: yes" nor "tty: yes". Try adding these  
and see whether that helps.

Cheers,  
Felix
