Deploy Docker Containers with Terraform¶
Table of Contents¶
- Deploy Docker Containers with Terraform
- Setting Up Terraform
- Project Setup
- Creating a Deployment
- Deploy 3 containers that are bound internally on port 80 and externally use 8080, 8081, and 8082.
- tl;dr
Setting Up Terraform¶
Your team has decided to use Terraform and deploy containers in an infrastructure as code manner.
Verify that containerd
is running and that Terraform is installed on your system.
Hint:
sudo systemctl status containerd
- The build infrastructuredocs
Project Setup¶
Make sure all the prerequisites are met.
containerd
is runningterraform
is installed
Check that containerd is running and exposed on your system¶
Check that Terraform is installed on your system¶
Make a Directory for your Terraform Project Files¶
Make sure there are no other Docker containers running¶
Creating a Deployment¶
Let's create the first deployment of Terraform in our environment.
- Create a Terraform configuration for running
nginx
on port 8000 on your system. - Verify that you can see the running container.
- View your
terraform.tfstate
file to see what Terraform tracks in a configuration deployment.
-
Make a
main.tf
file in the directory -
Add a default configuration for the terraform project
terraform { required_providers { docker = { source = "kreuzwerker/docker" version = "~> 2.13.0" } } } provider "docker" {} resource "docker_image" "nginx" { name = "nginx:latest" keep_locally = false } resource "docker_container" "nginx" { image = docker_image.nginx.latest name = "tutorial" ports { internal = 80 external = 8000 } }
-
Make sure the Terraform configuration is well formatted and validated.
We will see an error here.
This is because the validate is looking to see if we've done an init
and pulled down the provider.
That is happening in the next step.
-
Before we can deploy with Terraform we need to initialize and download all providers.
-
Let's check the system to see all the files that have been created
-
Let's deploy our resources
-
Let's verify that we have a working container
-
Look at the
WARNING: The terraform.tfstate file should never be edited by hand, only terraform should edit that file.terraform.tfstate
file to see all the objects that terraform is tracking in the deployment.
Your team is very impressed that you were able to deploy one container with Terraform and Docker. Now they want you to deploy multiple containers for different ports on the system.
Deploy 3 Containers¶
Deploy 3 containers that are bound internally on port 80 and externally use 8080, 8081, and 8082.
-
Start by destroying our old Terraform configuration.
-
Then edit for our new configuration.
Set the configuration to look like this:terraform { required_providers { docker = { source = "kreuzwerker/docker" version = "~> 2.13.0" } } } provider "docker" {} resource "docker_image" "nginx" { name = "nginx:latest" keep_locally = false } resource "docker_container" "nginx8080" { image = docker_image.nginx.latest name = "nginx8080" ports { internal = 80 external = 8080 } } resource "docker_container" "nginx8081" { image = docker_image.nginx.latest name = "nginx8081" ports { internal = 80 external = 8081 } } resource "docker_container" "nginx8082" { image = docker_image.nginx.latest name = "nginx8082" ports { internal = 80 external = 8082 } }
-
Make sure the Terraform configuration is well formatted and validated.
-
Deploy our resources
-
Verify that all the containers are working.
tl;dr¶
-
Check that
containerd
is running and exposed (i.e., listening) on your system. -
Check that
terraform
is installed. -
Create a directory named
learn-terraform-docker-container
. -
Change into the directory.
-
Create a
main.tf
file to define your infrastructure. -
Set a configuration in
main.tf
terraform { required_providers { docker = { source = "kreuzwerker/docker" version = "~> 3.0.1" } } } provider "docker" {} resource "docker_image" "nginx" { name = "nginx:latest" keep_locally = false } resource "docker_container" "nginx" { image = docker_image.nginx.image_id name = "tutorial" ports { internal = 80 external = 8000 } }