Ansible docker_container task cannot start a container while the equivalent docker run command can

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 ?

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
  docker_container:
        name: name
        image: image
        state: started
        restart_policy: "unless-stopped"
        volumes:
           - /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