Image for post
Image for post

Docker the Ansible Way — Part 2

Solution for Orchestrating Docker Containers with Ansible

In the previous article, I presented the problem to how to orchestrate WordPress and MySQL 5.7 containers using Ansible. I also gave an example InSpec script that you can use to validate the solution, as you develop it.

Previous Article

Problem and Verification Script

Full Solution Source

You can download this and run it to see the results:

GIST=https://gist.githubusercontent.com/darkn3rd/93e0fa294baf2bc8bbe642d4a48188f7/raw/0e88da8a39781313fb8e6f9c28cb30d15511bcab/docker_run.ymlcurl -O $GIST
chmod +x docker_run.yml
./docker_run.yml

Code Walkthrough Notes

Here’s my explanation on the different parts of this script.

Variables

The first phase step in this framework is to set up variables that I would use throughout the playbook, and fetch the environment variable WORDPRESS_PORT.

"{{ lookup('env','WORDPRESS_PORT') | default(8080)}}"

Volume Task

We create our persistent volume with docker_volume:

- name: "Create a Volume"
docker_volume:
name: "{{ docker_volume }}"

Network Task

We create our private container network with docker_network:

- name: "Create a network"
docker_network:
name: "{{ docker_network }}"

MySQL Container Task

We launch our database container using mysql:5.7 image, use a restart policy of always, use our volume db_data, use our network of wordpress_net, and configure the database by passing environment variables to the container.

- name: "Launch database container"
docker_container:
name: "{{ db_name }}"
image: mysql:5.7
volumes:
- "{{ docker_volume }}:/var/lib/mysql:rw"
restart_policy: always
network_mode: "{{ docker_network }}"
env:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

WordPress Container Task

We launch our web application WordPress container using the image wordpress:latest, use a restart policy always, use our network of wordpress_net, map the container port of 80 to a host port we can access on our workstation, and configure WordPress app by passing environment variables to the container.

- name: "Launch wordpress container"
docker_container:
name: "{{ wp_name }}"
image: wordpress:latest
ports:
- "{{ wp_host_port }}:{{ wp_container_port }}"
restart_policy: always
network_mode: "{{ docker_network }}"
env:
WORDPRESS_DB_HOST: "{{ db_name }}:3306"
WORDPRESS_DB_PASSWORD: wordpress

Final Notes

This was tested on both Ansible 2.7 and Ansible 2.8, and should work for Ansible 2.2 and above. I hope this was useful for both understanding Docker and Ansible, as well as using InSpec if you used that to verify the results.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store