Search code examples
dockeransibleprovisioning

What are the benefits of provisioning a Docker container with Ansible instead of a Dockerfile?


I am currently researching on two alternative approaches to provision Docker containers. Containers are currently managed with Ansible.

Approach 1: write a Dockerfile

Normally I would write a Dockerfile for my needs, build an image and then use that image to spin up a container.

Approach 2: use Ansible

  1. Write a minimal Dockerfile that sets up SSH access
  2. Provision the container with Ansible from a control machine, applying the relevant roles
  3. Build the Docker image

Quick comparison

With the second approach, I could reuse roles in other contexts, regardless of Docker. For example I could apply them to an EC2 instance or to a bare metal server hosted in-house.

One big loss seems to be Docker "layerization", in favour of an (arguably) more powerful (Ansible modules, idempotency, usually no bash scripting) provisioning experience.

I believe there is more I did not consider.

In case of approach 2, what am I missing from a (more traditional) approach 1? Is there any other better approach to this problem involving both Ansible and Docker?

PS: not strictly relevant to the question, but maybe worth mentioning: I would like to manage dev environments involving these Docker containers with Vagrant.


UPDATE #1

Approach 3: Packer + Ansible

Use Packer with Docker builder and Ansible provisioner (see @polarisuser answer)

In my opinion this can be seen as a refinement of approach 2.


Solution

  • A 3rd option that you might want to look in to is using Packer to create you image. I am currently using Packer and I really like it.

    What I do is I use Packer to pull the docker image that I want, and then in the provisioning step, I use Chef to get my image in a desired state. You could do the same thing with Ansible.