CHAPTER 12
Terraform and Docker
Updated: May 15, 2026
25 min read
# CHAPTER 12
Terraform and Docker
1. Introduction
While Terraform is famous for managing massive cloud providers like AWS and Azure, its architecture is provider-agnostic. If a technology has an API, Terraform can manage it. Docker is the industry standard for packaging and running applications. Instead of clicking buttons in the Docker Desktop GUI or typing longdocker run commands in the terminal, we can use Terraform to mathematically define our local and remote containerized environments. In this chapter, we will explore the Terraform Docker provider, learning how to pull images, orchestrate containers, and manage local infrastructure entirely through code.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand how Terraform interacts with the Docker Daemon API.
-
Configure the
kreuzwerker/dockerprovider.
- Use Terraform to pull Docker images from remote registries.
- Provision and configure Docker containers using HCL.
- Manage container networking and port mapping via Terraform.
3. Beginner-Friendly Explanation
Imagine organizing a shipping yard.- The Old Way (Docker CLI): You stand in the yard with a megaphone, yelling: "Bring me a blue shipping container! Put it on port 80! Now bring me a red container!" If you go home and come back the next day, you have to yell all the instructions again to get the same setup.
- The Terraform Way: You write a manifest (HCL code) listing exactly which containers you want and where they should sit. You hand the manifest to the yard manager (Terraform). The manager reads the list, moves the containers perfectly into place, and ensures they never move.
4. Configuring the Docker Provider
To manage Docker, Terraform needs to talk to the Docker Daemon running on your computer (or a remote server). We use the community-maintainedkreuzwerker/docker provider.
hcl
5. Managing Images and Containers
Once connected, we can define Docker Images (the blueprint) and Docker Containers (the running application).
hcl
6. Mini Project: Deploy Docker Containers using Terraform
Let's build a multi-container environment: A WordPress website connected to a MySQL database, entirely orchestrated by Terraform on your local machine.Step-by-Step Architecture Concept:
hcl
*Run terraform apply. Open your browser to localhost:8080. You will see a fully functioning WordPress installation! When you are done, run terraform destroy to instantly wipe the database, the website, and the network.*
7. Real-World Scenarios
A development team was struggling with local environments. Getting a new developer's laptop set up with the correct versions of PostgreSQL, Redis, and Elasticsearch took two days of reading outdated Wiki guides. The DevOps engineer deleted the Wiki and wrote a singlemain.tf file using the Docker provider. New developers simply cloned the repository and ran terraform apply. Terraform automatically reached out to Docker, downloaded the exact container versions, configured the port mappings, and established the internal networking in 60 seconds, perfectly matching the production environment.
8. Best Practices
-
Terraform vs. Docker Compose: The mini-project above looks very similar to a
docker-compose.ymlfile. If you are *only* managing local containers,docker-composeis usually better. However, if your architecture requires creating an AWS RDS Database, an S3 bucket, *and* a local Docker container for testing, Terraform is vastly superior because it can orchestrate all three providers simultaneously in the exact same state file.
9. Security Recommendations
-
Registry Authentication: In our examples, we pulled public images from Docker Hub. In an enterprise, images are stored in private registries (like AWS ECR). You must explicitly configure the
dockerprovider block with aregistryauthblock, supplying the username and password required to securely pull your company's proprietary code.
10. Troubleshooting Tips
-
Socket Permissions: On Linux/Mac, the most common error is
Cannot connect to the Docker daemon. This means Terraform does not have permission to talk to thedocker.sockfile. Ensure Docker Desktop is running, and that your user is added to thedockeruser group on your operating system.
11. Exercises
-
1.
What is the purpose of the
keeplocally = falseargument in thedockerimageresource?
- 2. How does Terraform establish communication between two separate Docker containers using HCL?
12. FAQs
Q: Can I use Terraform to build the Docker image from a Dockerfile? A: While possible using nullresources and local-exec provisioners, it is an anti-pattern. Terraform is an *infrastructure* orchestrator, not a *build* tool. You should use a CI/CD pipeline (like GitHub Actions) to rundocker build and push it to a registry. Terraform should then pull the pre-built image.
13. Interview Questions
-
Q: Contrast the operational use cases of
docker-composeversus the Terraform Docker provider. When would an engineering team prioritize one tool over the other?
- Q: Describe the HCL configuration required to provision a multi-container architecture where a frontend container securely communicates with a backend database container via an isolated Docker network.