# Design ideas ansible provision with docker compose

**URL:** <https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517>\
**Category:** Ansible Project\
**Created:** [January 20, 2017, 3:58pm UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517 "2017-01-20T15:58:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sonny\_Heer](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sonny\_Heer](https://forum.ansible.com/u/Sonny_Heer)\
**Post date:** [January 20, 2017, 3:58pm UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/1 "2017-01-20T15:58:09Z")

</div>

Goal:

1. use same roles regardless of container, VM, bare metal, swarm, or other cloud provider
2. easily add/remove nodes (containers)
3. use existing docker-compose files

I can’t seem to find an example of docker\_service which has ansible provisioning after container is up. e.g. I want to keep the containers very basic (bare centos), and use ansible to install all packages/configure. I’m assuming this happens first by defining the containers and then connecting to those containers from host machine (in my case a unix VM). Let me know if this makes sense or if more info is needed…

Thanks

---

<div class="post-metadata">

**Author:** ![Bejames](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bejames/32/4518_2.png) [@Bejames](https://forum.ansible.com/u/Bejames)\
**Post date:** [January 21, 2017, 2:47am UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/2 "2017-01-21T02:47:00Z")

</div>

If you are trying to use containers this way ( ansible provisioning inside running container) you are pushing against the philosophy behind containers. My suggestion is to use ansible to construct the files used to create containers. That way you get the control / power of ansible without trying to shoehorn it into the docker world.

For example I have a Dockerfile.j2 template that I pump a list of packages I want to install into it using ansible. Then I just run docker build.

eg  
(partial \<role\>/templates/Dockerfile.j2)

RUN DEBIAN\_FRONTEND=noninteractive apt-get -y update && apt-get -y install \  
&nbsp;&nbsp;&nbsp;&nbsp;{{ container\_packages | join (" ") }} \ \<\<\<\< Magic happens here  
&nbsp;&nbsp;&nbsp;&nbsp;&& rm -rf /var/lib/apt/lists/\*

and  
(\<role\>/vars/mail.yml)  
container\_packages:  
&nbsp;&nbsp;- apache2  
&nbsp;&nbsp;- curl  
&nbsp;&nbsp;- git  
&nbsp;&nbsp;- mysql-client  
&nbsp;&nbsp;- php5  
&nbsp;&nbsp;- php-apc  
&nbsp;&nbsp;- php5-curl  
&nbsp;&nbsp;- php5-gd  
&nbsp;&nbsp;- php5-json  
&nbsp;&nbsp;- php5-mysql  
&nbsp;&nbsp;- php5-sqlite  
&nbsp;&nbsp;- sqlite

---

<div class="post-metadata">

**Author:** ![Sonny\_Heer](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sonny\_Heer](https://forum.ansible.com/u/Sonny_Heer)\
**Post date:** [January 23, 2017, 1:24am UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/3 "2017-01-23T01:24:34Z")

</div>

Thanks for that info. I do see what you mean. I’d like to reuse existing ansible roles if possible. Assuming I’m only doing basic things like setting up a yum repo and installing packages on groups of nodes. Here is the lifecycle of what I’m thinking:

1. start up a configurable set of base containers (base os only).
2. run ansible to setup repos/ install yum packages
3. commit changes to snapshot the “configured images”
4. all the above is for initial setup - subsequent calls would call another play book to simply start the set of containers.

sample of testing this use case:

- name: Create a network  
docker\_network:  
name: dev
- name: test out docker service  
docker\_container:  
image: centos:7  
name: “node{{ item }}”  
state: started  
interactive: yes  
networks:
- name: dev  
with\_sequence: count=2

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [January 23, 2017, 9:14am UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/4 "2017-01-23T09:14:27Z")

</div>

Hi,

If you want to run containers, but not necessarily docker - have a look at lxd/lxc. I currently use it exactly the way you mentioned - reusing roles and configs and simply pointing to different environments.

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 23, 2017, 10:18am UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/5 "2017-01-23T10:18:52Z")

</div>

You are describing system containers, while docker does represent  
application containers. Docker containers only contain the one  
application that you want them to run. System containers are full  
systems running with init and sshd and whatnot.

You might want to checkout lxc and the lxc\_container module of ansible...

Johannes

---

<div class="post-metadata">

**Author:** ![Sonny\_Heer](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sonny\_Heer](https://forum.ansible.com/u/Sonny_Heer)\
**Post date:** [January 23, 2017, 3:00pm UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/6 "2017-01-23T15:00:48Z")

</div>

Each container will probably be running a single application or a few services. the dependencies to run said application need to be installed and configured. this is the part I’d like to provision via ansible. I am leaning towards a templatized dockerfile (not ansible, but still close). I didn’t know about lxc\_containers - i will check those out as well. although I believe what I’d like to do with containers is within docker’s ability.

---

<div class="post-metadata">

**Author:** ![Sonny\_Heer](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sonny\_Heer](https://forum.ansible.com/u/Sonny_Heer)\
**Post date:** [January 23, 2017, 4:27pm UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/7 "2017-01-23T16:27:46Z")

</div>

do you have examples of using lxd/lxc in this use case? initial start - provision - subsequent starts have provisioning

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [January 23, 2017, 7:14pm UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/8 "2017-01-23T19:14:20Z")

</div>

Hi,

LXC containers that I use have cloud-init build in, so that the main way of configuring them.

All example below use LXD as the ‘hypervisor’. I spin up a container with one basic profile (‘bootstrap’) (bound to a network that has full internet access), configure the container (i.e. apply all my roles) and then reconfigure it to the target profile (which usually means different network/IP) and restart.

Tasks to create a container profile:

- name: create a service profile  
lxd\_profile:  
name: service  
description: “used for services containers”  
state: present  
devices:  
eth0:  
name: eth0  
nictype: bridged  
parent: vlan3  
type: nic

- name: create a bootstrap profile  
lxd\_profile:  
name: bootstrap  
description: “used for bootstrapping of containers”  
state: present  
config: { “user.user-data”: “#cloud-config\nssh\_authorized\_keys:\n - ssh-rsa AAAAB3xxxxx\npackages:\n - openssh-server”}  
devices:  
eth0:  
name: eth0  
nictype: bridged  
parent: vlan2  
type: nic

Playbook to spin up a container and configure it. I pass the final role as a parameter. “gather facts” is off since the container images don’t have python by default and I install it manually using role “common”.

- name: create container  
hosts: “{{ lxdhost | default(t1) }}”  
connection: ssh  
user: pshemk  
become: true  
tasks:

- name: build container  
register: result  
lxd\_container:  
name: “{{ lxcname }}”  
state: started  
source:  
type: image  
properties:  
os: “ubuntu”  
release: “xenial”  
architecture: “amd64”  
profiles: [“bootstrap”]  
timeout: 600  
wait\_for\_ipv4\_addresses: true

- name: update local inventory  
delegate\_to: 127.0.0.1  
connection: local  
become: false  
copy: content=“[{{ lxcname }}]\n{{ result.addresses.eth0[0] }} type=lxc” dest=“./inventory/dyn-{{ lxcname }}”

- meta: refresh\_inventory

- pause: seconds=60

- name: setup container  
hosts: “{{ lxcname }}”  
connection: ssh  
user: ubuntu  
become: true  
gather\_facts: false  
roles:

- common

- resolver

- “{{ lxcrole }}”

- name: restart container  
hosts: “{{ lxdhost }}”  
connection: ssh  
user: pshemk  
become: true  
tasks:

- name: reasign profile  
register: result  
lxd\_container:  
name: “{{ lxcname | default(totara) }}”  
state: restarted  
profiles: [“{{ hostvars[inventory\_hostname][‘hosts’][lxcname][‘profile’] }}”]  
timeout: 600  
wait\_for\_ipv4\_addresses: true

- name: update local inventory  
delegate\_to: 127.0.0.1  
connection: local  
become: false  
copy: content=“[{{ lxcname }}]\n{{ result.addresses.eth0[0] }} type=lxc” dest=“./inventory/dyn-{{ lxcname }}”

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Sonny\_Heer](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sonny\_Heer](https://forum.ansible.com/u/Sonny_Heer)\
**Post date:** [January 23, 2017, 11:14pm UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/9 "2017-01-23T23:14:58Z")

</div>

Thanks Pshem - lots of details. I think after thinking over this with a colleague, I plan to go with docker mainly because of future usage (compose / swarm / existing container projects within company).

Curious on how to best utilize a docker jinji template in ansible workflow of starting containers. any ideas welcome.

---

<div class="post-metadata">

**Author:** ![Sonny\_Heer](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sonny\_Heer](https://forum.ansible.com/u/Sonny_Heer)\
**Post date:** [January 23, 2017, 11:24pm UTC](https://forum.ansible.com/t/design-ideas-ansible-provision-with-docker-compose/23517/10 "2017-01-23T23:24:51Z")

</div>

j2 template (jinja2)\*
