How to run a Docker Container as a daemon with Ansible?

Hello to everyone,

I came across this issue and until now I haven’t found a practical solution.

When running normally on my docker host this commad: docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock -t dalexiou/nginx_docker-gen

I am spinning up a container as a daemon.

Now I have a playbook to automate this process. This playbook creates a directory on the host I want to spin up the container, puts inside the Dockerfile. Executes the Dockerfile to build the image and finally run the image to start a daemon container.

Hi Dimitrios,

The docker module works fine and by default the daemon option is true.
The command/entrypoint of your Dockerfile must never return if you want to run as daemon.
It has nothing to do with Ansible.
First do the following:

  • run your docker process via command line (in daemon mode)
  • check that it runs properly (docker ps -a)
  • check the logs (docker logs <container_id>)

I did the same mistake when using docker. For instance to run mysql I thought running “service mysql start” would do the trick. But the “service” command starts the mysqld_safe process and returns. So even if I ask for a daemon mode it will always return.
The way to go was to run the /usr/bin/mysqld_safe command.
Check that you run the daemon process, not a wrapper around it: you should get better results.

Regards,

Louis