nieve
nieve

Reputation: 5117

Managing docker containers with ansible - best practices

I'm considering using both docker and ansible. The idea I had was to use ansible to set up my instances and I was wondering what would be the best practice to do so:

  1. Calling ansible from the dockerfile on every container (which would necessitate having ansible installed on every container/instance. This method is mentioned in the ansible up and running book, on the docker episode); or
  2. Running my containers and then setting up all the instances by executing ansible-playbook.

What would be the best approach? Are there any other alternative ways for such use case?

Upvotes: 5

Views: 2701

Answers (1)

Jonathan
Jonathan

Reputation: 705

To use Ansible to setup your docker hosts (i.e. instances), you don't need to install Ansible on the remote machine. You install Ansible on your primary machine, and run playbooks and ad-hoc commands from there. This is why Ansible is a good tool for this kind of task (i.e. installing things on remote machines).

For example, if your remote docker host is a CentOS 7 machine, you could use the following playbook to install docker based on the Docker install directions

- name: Install Docker on remote hosts
  hosts: docker-hosts
  sudo: yes
  tasks:
    - name: Install docker
      shell: curl -sSL https://get.docker.com | sh

Note that the docker-hosts group is defined by your hosts/inventory file.

Once you have docker installed on the remote machines, you can create another Ansible playbook to create/run your containers.

We commonly use the Ansible shell module in lieu of the docker module. This is more for convenience and reference. So later on, someone can look at the shell command you use to deploy containers remotely as an example for their own development (i.e. "How do you run that 'docker run' command again?")

Upvotes: 4

Related Questions