Image for post
Image for post

Docker the Hard Way with WordPress

Orchestrating WordPress-MySQL containers w. Docker command

Recently I was helping a friend get rapidly up to speed in Docker, and developed this small lab, which I am sharing in hopes this might be helpful in your Docker learning activities.

Why WordPress

About the Tools Required

Docker consists of many components, but for this lab we mainly focus on the docker client tool, simply called docker. The docker client will communicate to a docker service running on a Linux server.

Getting Docker on macOS (aka Mac OS X)

You can set all of this up with either Docker Desktop or Docker Machine (or both). If you have Homebrew installed, you can get the components with these commands:

Getting Docker on Windows 10

You can set all of this up with either Docker Desktop or Docker Machine. If you have Chocolatey installed, you can install either of these tools with:

Getting Docker on Linux

On Linux, you can follow instructions for Docker CE (Community Edition).

The Lab Challenge

The goal of this exercise is to create a shell script (or whatever you would like to use for automating command lines) to automate using the docker command and do the following below, divided into basic and advanced features.

Basic Features

For the basics, we simply want to launch the containers:

Image for post
Image for post
  1. Launch WordPress (wordpress:latest) with restart policy of always, container name of wordpress, and map the container port 80 to a host port of 8080, and pass environment variables to the container that will trigger the container’s internal self-configuring automation.

Advanced Features

For these features, we want to use a private network, have a persistent volume for the database, and allow the user to specify the host port using an environment variable WORDPRESS_PORT.

Image for post
Image for post
  1. Create a volume called db_data, and mount the volume as /var/lib/mysql in the MySQL db container.
  2. For the wordpress container, set the host port to the environment variable WORDPRESS_PORT, and if this is not set, then default it to port 8080.

Further Notes

For this exercise, you do not need to craft any Dockerfile for either WordPress or MySQL containers. Use the official Docker images mysql:5.7 and wordpress:latest.

  1. Clean Up the running containers (see clean up below).
  2. Run the script again, but this time with the environment WORDPRESS_PORT set to another value, e.g. 8888, and then navigate your web browser to the same address (in step 1) with this port, e.g. localhost:8888.

Clean Up

To stop and remove the containers, you can the following:

docker ps -f name=wordpress -q | xargs docker stop && \
docker ps -f name=wordpress -aq | xargs docker rm
docker ps -f name=db -q | xargs docker stop && \
docker ps -f name=db -aq | xargs docker rm
docker volume rm db_data
docker network rm wordpress_net

Verifying Solution

If you have the tool Inspec installed, you can use this test case to validate the environment and practice systems level TDD.

# run your solution
./docker_run
# verify your solution
inspec
exec container_test.rb
# run your solution
export WORDPRESS_PORT=8888
./docker_run
# verify your solution
echo "port: $WORDPRESS_PORT" > attributes.yml
inspec exec container_test.rb --attrs=attributes.yml
Image for post
Image for post
Inspec Test Results of Successful Solution

Until Next Time…

That’s the the lab, and if you are unfamiliar with Docker, this will require some research and tinkering. For those familiar with Docker, this may be a good lab to review commands, and also to compare and contrast to something like Docker Compose, that is easier to use than raw docker commands.

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