Image for post
Image for post

Docker the Easy Way

Orchestrating containers with Docker Compose

Before I had some articles on orchestrating local WordPress and MySQL containers with Docker CLI automated in Bash, Python, Ruby, and Perl, using Docker Python SDK, and also Ansible.

These are good for understanding, writing programs that interact with the docker service or for integrating with existing automation, but really,

Is this the easiest solution for local development?

Enter Docker Compose

There’s an easier way to do this, with a little small tool that was once called Fig, and later when acquired by Docker, it matured into what is now Docker Compose.

Docker Compose simply is an orchestration tool for Docker that uses an intuitive DSL (Domain Specific Language) in YAML format. With this, you can run or compose a number of containers and dependent docker resources.

For this exercise, like the previous articles, we will launch a WordPress container and a MySQL 5.7 container that is used by the WordPress service. The specific requirements are below.

Problem Challenge

The goal of this is to use Docker Compose to orchestrate WordPress and MySQL containers. We would like to ultimately use a persistent volume (so that data is preserved between restarts), have the containers communicate on their own private network, always restart should they crash for any reason, and have a user specified port to access our the application.

Create a two support two services called and . These declarations will bring up the WordPress () and MySQL () containers.

We’ll divide these requirements into four stages:

Stage 1: Just the Containers

These features will require Docker Compose file version 2 or greater.

  1. Create a service, using the image that will have a restart policy of always, name the container as , and configure the WordPress database as well as administrative root password (hint: pass environment vars to the container)
  2. Create a service, using the image that will have restart policy of always, name the container as , map the container port to the host port of , configure hostname of database and user and password to the MySQL database used by the WordPress app. (hint. pass environment vars to the WordPress container)

Here’s a table of the features:

Image for post
Image for post

Some magic happening at this stage is that Docker Compose will automatically place these containers into their own private network, under a name that is automatically generated.

Stage 2: Persistent Volume

Do the following:

  1. Create a volume labeled for use with the service.
  2. Alter the db service to have mount the volume, to where the MySQL database resides, which is .

When we create the volume, it is not really called , instead Docker Compose generates a name using the db_data string. This way, if we have two projects using the same volume name, they will not collide.

Stage 3: Environment Variable

We want to allow the user to specify the host port using the environment variable:

  1. Configure the wordpress service to use the environment variable for the host port, and if it is not set, default it to . (Hint: create a file, see references)

Stage 4: Explicit Network and Volume Name

Normally you will let Docker Compose configure the volume name and network name, but with at least version 3.7, you can use your own network name. This allows for interoperability with other projects that may not be managed by Docker Compose.

  1. When creating a volume, explicitly specify the name and mark that Docker Compose will create this.
  2. Create a network explicitly named , and specify that Docker Compose will create this.
  3. Configure both services to use the network .

Here’s a table of the final features completed for this last stage:

Image for post
Image for post

Getting Started

Below are some notes to get started:

Starter Script

Running the Script

Download the script as , and then run it by:

docker-compose up -d

For specifying a custom port you can do this:

export WORDPRESS_PORT=8888
docker-compose up -d

Verification

You can use this Inspec script to verify the results are correct. This requires you to implement the advance features. Otherwise, you can run this and ignore failures with JSON , and .

To run this script, and Inspec installed, type:

docker-compose up -d
inspec
exec container_test.rb

When using a custom port with , you can do this:

export WORDPRESS_PORT=8888
docker-compose up -d
echo "port: $WORDPRESS_PORT" > attributes.yml
inspec exec container_test.rb --attrs=attributes.yml

Resources

Docker Compose References

Formal Requirements

Last Thoughts

That’s it for this article, just the problem challenge and a small starter script.

I will follow up with a solution in the second part of this article.

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