To “docker-machine” or not to “docker-machine” is that even a question?!

Well I have to say the “native clustering” support that came out with docker machine is a ״Double-edged sword״ ;) … On one hand it is indeed very easy to spinup a swarm cluster, but on the other if you don’t use docker-machine to do that it is just a nightmare to configure considering all the TLS / authentication configuration needed to be done in order to get this to work.

In our use case I was looking on spawning a swarm cluster running on an existing environment already utilizing consul and registrator to find that unless I use docker-machin’s “built in” swarm spawning capabilities I cannot take advantage of swarm ( or work very hard to get it to work …), don’t get me wrong this post doesn’t mean you shouldn’t use docker-machine but a caveat to be aware of when planning to.

So how did I use docker-machine?

  1. Create a consul cluster for development & production looks something like:

- name: Create consul cluster (docksul01-03)
  shell: "docker-machine ls | grep {{ item }} || docker-machine create {{ item }} --driver {{ docker_driver }} --engine-opt dns=127.0.0.1 --engine-opt dns={{ external_dns_first }} --engine-opt dns={{ external_dns_second }} --virtualbox-memory \"{{ consul_virtualbox_memory }}\""
  with_items:
    - "docksul01"
    - "docksul02"
    - "docksul03"
   
This will yield (well + the 3 consul containers ...) with a 3 node container based consul cluster on my Virtualbox exposed on my local machine
  1. Creating my application’s infrastructure on-top a consul cluster running registrator against my docker-swarm with something like:

- name: Create AS swarm cluster manager
  shell: "docker-machine ls | grep {{ item }} || docker-machine create {{ item }} --driver {{ docker_driver }} --swarm --swarm-master --swarm-discovery consul://{{ dockerip_docksul01 }}:{{consul_port}}/swarm --engine-opt dns={{ external_dns_first }} --engine-opt dns={{ external_dns_second }} --virtualbox-memory \"{{ as_virtualbox_memory }}\""
  with_items:
    - "as01"
  tags:
    - swarm

- name: Create AS swarm cluster members
  shell: "docker-machine ls | grep {{ item }} || docker-machine create {{ item }} --driver {{ docker_driver }} --swarm --swarm-discovery consul://{{ dockerip_docksul01 }}:{{consul_port}}/swarm --engine-opt dns={{ external_dns_first }} --engine-opt dns={{ external_dns_second }} --virtualbox-memory \"{{ as_virtualbox_memory }}\""
  with_items:
    - "as02"
    - "as03"
    tags:
    - swarm
    - swarm-members

So what did I get out of docker-machine? Not as much as I expected to saty the truth but still enough to keep using it ;)

I can easily query the docker-machine api for members IP, running container etc Which made it super easy to use in ansible for example:


- shell: "docker-machine ip as01"
  register: dockerip_as01

which in tern could be registered as a fact I can use to communicate with my docker hosts like so:


- name: launch registrator container on as01
  docker:
    name: registrator01
    hostname: registrator01
    image: gliderlabs/registrator:{{ registrator_tag }}
    state: started
    use_tls: encrypt
    memory_limit: "128MB"
    docker_url: "tcp://{{ dockerip_as01 }}:{{ docker_port }}"
    volumes: /var/run/docker.sock:/tmp/docker.sock
    restart_policy: always
    dns:
      - "{{ dockerip_docksul03 }}"
      - "{{ external_dns_first }}"
      - "{{ external_dns_second }}"
    command: consul://{{ dockerip_docksul01 }}:{{ consul_port }}
  tags:
    - registrator

To summarize:

Docker-machine is really a cool set of wrappers which can be very useful during deployments or even setting up clusters, unfortunately there aren’t flexible enough IMO ATM but I assume this will imrpove.

I will be publishing a series of articles + code samples which will prove why/how ansible + docker == love ;) If you have any questions / findings you know where to find me … ;)

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com