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

374 views
Skip to first unread message

Dimitrios Alexiou

unread,
Jan 7, 2015, 12:02:36 PM1/7/15
to ansible...@googlegroups.com
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.

---
- name: Create the nginx directory with the Dockerfile.
  file: path=/home/finch/nginx state=directory

- name: Copy the Dockerfile to the created nginx directory.
  copy: src=Dockerfile dest=/home/finch/nginx/

- name: Build the nginx_docker-gen image from the copied Dockerfile.
  docker_image: path="/home/finch/nginx/" name="dalexiou/nginx_docker-gen" state=present

- name: Run nginx_docker-gen container(s) from the image created before.
  docker:
    image: dalexiou/nginx_docker-gen
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock
    name: nginx_lb
    state: running

The problem is that the playbook works fine with no errors. I can spin up the docker container but not as a daemon. Is anyone coming across with the same issue as well?

Thanks in advance :-)

louis gueye

unread,
Jan 19, 2015, 4:43:31 AM1/19/15
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages