Image for post
Image for post

Docker the Ansible Way

Orchestrating Docker Containers with Ansible

Ansible is powerful tool that does orchestration, including orchestrating Docker containers through a simple its own DSL (domain specific language) represented in YAML file format.

Related Articles

Problem Requirements for Docker Command

Solution in Shell Script for Docker Command

Prerequisites

You need to have at minimum Docker installed, Python environment installed, and these python packages:

Article on Python Environments using PyEnv

This is an earlier article about managing Python versions using pyenv tool. This may be useful for setting up initial environment needed for docker and ansible python modules.

The Problem

The goal of this project is to launch two containers (WordPress and MySQL) on a private network, with persistent database, and accessible by a user specified port through WORDPRESS_PORT. Create an Ansible playbook, docker_run.yml, to the following listed below.

Basic Container Features

  1. Launch MySQL (mysql:5.7) with restart policy of always, use friendly container name of db, and configure the root password as well as database and user so that Wordpress app can use it (hint: pass env vars to container)
  2. Launch WordPress (wordpress:latest) with port map from container web port 80 to host port of 8080, restart policy of always, use friendly container name of wordpress, and configure the container to use the MySQL database (hint: pass env vars to container)
Image for post
Image for post

Advanced Container Features

We want to create and support network and volume resources, and allow the user to specify the host port.

  1. For both containers, use a private network wordpress_net.
  2. For the MySQL (mysql:5.7), use a persistent volume called db_data, and use this for the MySQL database.
Image for post
Image for post

Formal Requirements Document

Starter Script

I created this started script to get you started:

ansible-playbook docker_run.yml

The Verification

When you are finish, try your script using both the default port 8080, and a port of your choosing that is specified by the WORDPRESS_PORT.

Some Testing Is Just Not Needed

Any competent change configuration should be idempotent when configuring resources like docker volumes, networks, and containers. Thus, you do not need to code in behavior to check if you should not create things that exist, or code necessary checks that can cause an error. The docker modules in Ansible should handle such conditions.

Automated Validation

I wrote a script that can be used to valid the final solution, so you can apply TDD (Test Driven Design) to validate your final solution. You will need InSpec 3.x installed for this to work. If you install InSpec 4.x using Ruby gem install, you will need the inspec-bin in addition to inspec gems.

ansible-playbook docker_run.yml
inspec
exec container_test.rb
export WORDPRESS_PORT=8888
ansible-playbook docker_run.yml
echo "port: $WORDPRESS_PORT" > attributes.yml
inspec exec container_test.rb

Resources

Here are some links related to the exact modules you would utilize for this exercise:

Wrapping Up

In this article, I only wanted to present the problem, and I will follow-up with the solution.

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