docker_container moduel fails to keep alive consul container

68 views
Skip to first unread message

Agusti Tomas

unread,
Aug 18, 2016, 12:41:38 PM8/18/16
to Ansible Project
Hello,

I have a simple role called consul:

- name: Pull consul image
  docker_image:
    name: consul
    tag: latest
    state: present
   
- name: Start consul container
  docker_container:
    name: consul
    image: consul
    state: started
    published_ports:
      - 8500:8500
      - 8301:8301
      - 8300:8300
    hostname: "{{ ansible_hostname }}"
    command: -d --name consul consul agent -server -bootstrap -ui -client=0.0.0.0

Which in my tests I have slimmed down to:

- name: Pull consul image
  docker_image:
    name: consul
    tag: latest
    state: present

- name: Start consul container
  docker_container:
    name: consul
    image: consul
    state: started
    command: consul

And which fails to start, and keep started the consul container. This is the log for the container (/var/lib/docker/containers/889833.../889833...-json.log):

[root@buildmaster 88983353e734e11b700d0f480318d76a1565de6ab474ca7a2da66915917ec87b]# more 88983353e734e11b700d0f480318d76a1565de6ab474ca7a2da66915917ec87b-json.log
{"log":"usage: consul [--version] [--help] \u003ccommand\u003e [\u003cargs\u003e]\n","stream":"stderr","time":"2016-08-18T14:12:17.207230775Z"}
{"log":"\n","stream":"stderr","time":"2016-08-18T14:12:17.207282204Z"}
{"log":"Available commands are:\n","stream":"stderr","time":"2016-08-18T14:12:17.207287432Z"}
{"log":"    agent          Runs a Consul agent\n","stream":"stderr","time":"2016-08-18T14:12:17.207290787Z"}
{"log":"    configtest     Validate config file\n","stream":"stderr","time":"2016-08-18T14:12:17.207294201Z"}
{"log":"    event          Fire a new event\n","stream":"stderr","time":"2016-08-18T14:12:17.207297315Z"}
{"log":"    exec           Executes a command on Consul nodes\n","stream":"stderr","time":"2016-08-18T14:12:17.207300479Z"}
{"log":"    force-leave    Forces a member of the cluster to enter the \"left\" state\n","stream":"stderr","time":"2016-08-18T14:12:17.207303493Z"}
{"log":"    info           Provides debugging information for operators\n","stream":"stderr","time":"2016-08-18T14:12:17.207306719Z"}
{"log":"    join           Tell Consul agent to join cluster\n","stream":"stderr","time":"2016-08-18T14:12:17.207310573Z"}
{"log":"    keygen         Generates a new encryption key\n","stream":"stderr","time":"2016-08-18T14:12:17.207313436Z"}
{"log":"    keyring        Manages gossip layer encryption keys\n","stream":"stderr","time":"2016-08-18T14:12:17.2073163Z"}
{"log":"    leave          Gracefully leaves the Consul cluster and shuts down\n","stream":"stderr","time":"2016-08-18T14:12:17.207319309Z"}
{"log":"    lock           Execute a command holding a lock\n","stream":"stderr","time":"2016-08-18T14:12:17.207322275Z"}
{"log":"    maint          Controls node or service maintenance mode\n","stream":"stderr","time":"2016-08-18T14:12:17.207325095Z"}
{"log":"    members        Lists the members of a Consul cluster\n","stream":"stderr","time":"2016-08-18T14:12:17.207327983Z"}
{"log":"    monitor        Stream logs from a Consul agent\n","stream":"stderr","time":"2016-08-18T14:12:17.207330872Z"}
{"log":"    reload         Triggers the agent to reload configuration files\n","stream":"stderr","time":"2016-08-18T14:12:17.207333683Z"}
{"log":"    rtt            Estimates network round trip time between nodes\n","stream":"stderr","time":"2016-08-18T14:12:17.207336563Z"}
{"log":"    version        Prints the Consul version\n","stream":"stderr","time":"2016-08-18T14:12:17.207339435Z"}
{"log":"    watch          Watch for changes in Consul\n","stream":"stderr","time":"2016-08-18T14:12:17.207342305Z"}
{"log":"\n","stream":"stderr","time":"2016-08-18T14:12:17.207345139Z"}

So apparently the consul command I pass is not good (I also tried quotes around the command).

If I start the container manually with:

[vagrant@buildmaster ~]$ docker run -ti -p 8500:8500 -p 8301:8301 -p 8300:8300 --name consul consul agent -server -bootstrap -ui -client=0.0.0.0
==> WARNING: Bootstrap mode enabled! Do not enable unless necessary
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
         Node name: '6b02f67046a8'
        Datacenter: 'dc1'
            Server: true (bootstrap: true)
       Client Addr: 0.0.0.0 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
      Cluster Addr: 172.17.0.2 (LAN: 8301, WAN: 8302)
    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
             Atlas: <disabled>

==> Log data will now stream in as it occurs:

    2016/08/18 14:30:29 [INFO] serf: EventMemberJoin: 6b02f67046a8 172.17.0.2
    2016/08/18 14:30:29 [INFO] serf: EventMemberJoin: 6b02f67046a8.dc1 172.17.0.2
    2016/08/18 14:30:29 [INFO] raft: Node at 172.17.0.2:8300 [Follower] entering Follower state
    2016/08/18 14:30:29 [INFO] consul: adding LAN server 6b02f67046a8 (Addr: 172.17.0.2:8300) (DC: dc1)
    2016/08/18 14:30:29 [INFO] consul: adding WAN server 6b02f67046a8.dc1 (Addr: 172.17.0.2:8300) (DC: dc1)
    2016/08/18 14:30:29 [ERR] agent: failed to sync remote state: No cluster leader
    2016/08/18 14:30:30 [WARN] raft: Heartbeat timeout reached, starting election
    2016/08/18 14:30:30 [INFO] raft: Node at 172.17.0.2:8300 [Candidate] entering Candidate state
    2016/08/18 14:30:30 [INFO] raft: Election won. Tally: 1
    2016/08/18 14:30:30 [INFO] raft: Node at 172.17.0.2:8300 [Leader] entering Leader state
    2016/08/18 14:30:30 [INFO] consul: cluster leadership acquired
    2016/08/18 14:30:30 [INFO] consul: New leader elected: 6b02f67046a8
    2016/08/18 14:30:30 [INFO] raft: Disabling EnableSingleNode (bootstrap)
    2016/08/18 14:30:30 [INFO] consul: member '6b02f67046a8' joined, marking health alive
    2016/08/18 14:30:31 [INFO] agent: Synced service 'consul'

It works! Also if I daemonize it.

Anyone sees any problem? Thanks!

Kai Stian Olstad

unread,
Aug 19, 2016, 11:01:28 AM8/19/16
to ansible...@googlegroups.com
On 18. aug. 2016 16:31, Agusti Tomas wrote:
> Hello,
>
> I have a simple role called consul:
>
> - name: Pull consul image
> docker_image:
> name: consul
> tag: latest
> state: present
>
> - name: Start consul container
> docker_container:
> name: consul
> image: consul
> state: started
> published_ports:
> - 8500:8500
> - 8301:8301
> - 8300:8300
> hostname: "{{ ansible_hostname }}"
> command: -d --name consul consul agent -server -bootstrap -ui
> -client=0.0.0.0

Never used docker_container, but this command seems weird. command: is
the command to run inside the container, so "-d --name consul" should be
deleted?

--
Kai Stian Olstad

Werner Flamme

unread,
Aug 19, 2016, 11:20:21 AM8/19/16
to ansible...@googlegroups.com
Kai Stian Olstad [19.08.2016 17:01]:
According to
<http://docs.ansible.com/ansible/docker_container_module.html>, this is
"Command to execute when the container starts.". Looking at the examples
further down the page, you have to give the complete command, not only
some parameters.


--


Reply all
Reply to author
Forward
0 new messages