Image for post
Image for post

Docker the Terraform Way - Part 2

Solution for Orchestrating Docker Containers with Terraform

This is the second in this series, the solution for orchestrating docker containers (WordPress and MySQL) complete with a private network, persistent volume for the database, and ability to change the local host port for the WordPress application.

Previous Article


This solution has been by far the most intuitive and easy solution. I’ll just show the code, then offer any explanations as needed.

Source Code

Below is the source code to the solution

This solution is expressive and self documenting: we create volume resource, network resource, and then launch the containers.

Terraform has a controlled mechanism for inputs, either reading from the state for things you configure, or through input variables. We create an input variable wordpress_port with a default of 8080.

Running the Solution

Using the default host port, running this has not changed (as per instructions in the previous article):

terraform init  # download docker provider (only do first time)
terraform plan # list changes that will occur
terraform apply # apply the changes

To change the desired host (external) port, you can simply do this:

terraform apply -var="wordpress_port=8888"

Or use an environment variable:

export WORDPRESS_PORT=8888
terraform apply -var="wordpress_port=$WORDPRESS_PORT"


There you have it the problem challenge and the solution. This solution, I have to say, has to be the most intuitive, quickest and easiest solution, even easier than Docker Compose, especially with using custom variables that have defaults.

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