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.
This solution has been by far the most intuitive and easy solution. I’ll just show the code, then offer any explanations as needed.
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
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:
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.