Docker the Hard Way: Part 3

More Solutions for Orchestrating WordPress-MySQL containers

Joaquín Menchaca (智裕)
3 min readMay 23, 2019

--

In a previous article I detailed a solution using simple shell script to orchestrate launching WordPress and MySQL 5.7 containers.

I think a shell script is the best solution, as the automation is self-contained, either from internal automation baked into the container’s themselves or from the docker command, so it doesn’t really call for advanced capabilities of scripting languages like the ones I’ll introduce.

But if your heart is set to use Python, Ruby, or Perl for automation, read further. This is a survey of those languages and essentially demonstrate shell interaction, as an alternative to shell scripting.

Previous Articles

The Problem Defined

The Solution with Shell Script

The Solution in Python

Python is extremely popular, so I started with a solution in that language. This requires Python 3.6 or higher for the new f-string feature.

In Python, you can use the subprocess library to call out and interact with the shell. The input to these commands must be a list, not a string, so split() is used. I first format the commands to use later with the subprocess methods.

If pipelines are used, such as passing list of Docker IDs to an xargs command, then the proper way to handle this in Python is to use Popen() method. This is demonstrated above.

Some things that I like about Python:

  • defaulting vars with var = os.environ.get('VAR') or 'default_value'
  • matching with in operator

The new f-string feature is great, as you can do stuff like:

num = 5
word = 'howdy'
string = f"""
A number = {num}
A word = {word}
"""

Where before Python 3.6 you’d have to do this:

num = 5
word = 'howdy'
string = """
A number = {}
A word = {}
""".format(num,word)

The Solution in Ruby

Ruby is still quite popular for system administration chores, so here is that solution:

Personally, at least in this case, Ruby is easier to read, and I especially like Ruby for these syntax sugar reasons:

  • subshells crazy simple with backticks ``
  • matching is simple with match operator =~ or not-match operator !~
  • interpolation of variables or methods is simple with "#{var}"
  • global substitution with gsub() method from any string, including here-string.
  • defaulting variables with var = ENV['VAR'] || 'default_value'

The Solution in Perl

And we wouldn’t be complete, if we could not at least peek at Perl, if for not other reason to compare and contrast to others.

I always liked Perl for these syntax sugar reasons:

  • subshells crazy simple with backticks ``
  • matching is simple with match operator =~ or not-match operator !~
  • interpolation of variables or methods is simple with "$var"
  • global substitution similar to sed with $var =~ s/pattern/replace/g;.
  • defaulting variables with $var = ENV{'VAR'} || 'default_value';

Wrapping Up

There you have it, three languages (Python, Ruby, Perl), four if you include the shell script solution from previous article.

--

--

Joaquín Menchaca (智裕)
Joaquín Menchaca (智裕)

Written by Joaquín Menchaca (智裕)

DevOps/SRE/PlatformEng — k8s, o11y, vault, terraform, ansible

No responses yet