Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding the Docker Provider for Terraform

Understanding the Docker Provider for Terraform

In this talk, I look at the basics concepts of HashiCorp Terraform and explain them, using the Docker Provider.

This version of the presentation was given at a virtual event for Docker London in May 2020.

---

Companion Code: github.com/ksatirli/understanding-the-docker-provider-for-terraform

Kerim Satirli

May 27, 2020
Tweet

More Decks by Kerim Satirli

Other Decks in Technology

Transcript

  1. Agenda Introducing Terraform basic concepts Managing Docker with Terraform provisioning

    Images, Containers and Networks Building out expanding your knowledge
  2. HashiCorp Configuration Language CODE EDITOR service "http" "web_proxy" { listen_addr

    = "127.0.0.1:8080" process "server" { command = ["proxy-app", "server"] } } variable "port" { description = "Port for web_proxy" default = 8080 }
  3. Provider set-up CODE EDITOR provider "docker" { version = "~>

    2.7" host = "tcp://localhost:2376" registry_auth { address = "registry.hub.docker.com" config_file = "/Users/ksatirli/.docker/config.json" } }
  4. Command: terraform init TERMINAL > terraform init Initializing the backend...

    Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "docker" (terraform-providers/docker) 2.7.0... Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
  5. Docker Container CODE EDITOR resource "docker_image" "minecraft" { name =

    "hashicraft/minecraft:v1.12.2" } variable "image_tag" { type = string description = "Tag of Docker Image to pull" default = "v1.12.2" }
  6. Docker Container CODE EDITOR resource "docker_image" "minecraft" { name =

    "hashicraft/minecraft:${var.image_tag}" } variable "image_tag" { type = string description = "Tag of Docker Image to pull" default = "v1.12.2" }
  7. Command: terraform plan TERMINAL > terraform plan -out="docker.tfplan" Terraform will

    perform the following actions: # docker_image.minecraft will be created + resource "docker_image" "minecraft" { + id = (known after apply) + latest = (known after apply) + name = "hashicraft/minecraft:v1.12.2" } Plan: 1 to add, 0 to change, 0 to destroy.
  8. Command: terraform apply TERMINAL docker_image.minecraft: Creating... docker_image.minecraft: Still creating... docker_image.minecraft:

    Creation complete after 5s Apply complete! Resources: 1 added, 0 changed, 0 destroyed. The state of your infrastructure has been saved to the path below. This state is required to modify and destroy your infrastructure, so keep it safe. State path: terraform.tfstate
  9. Terraform State ▪ maps real-world resources to your configuration ▪

    keeps track of (resource) metadata ▪ improves performance for large infrastructures ▪ stored locally (by default), can be stored remotely
  10. Variable Definition Files CODE EDITOR image_tag = "v1.12.2" minecraft_world_backup =

    "https://git.io/Jfrmv" minecraft_mods_backup = "https://git.io/Jfrm9" minecraft_resource_pack = "https://git.io/JfrmH"
  11. Variable Definition Files ▪ contain key-value definitions of variables ▪

    automatically loaded if named: ▪ "terraform.tfvars" or "terraform.tfvars.json" ▪ "*.auto.tfvars" or "*.auto.tfvars.json"
  12. Docker Container CODE EDITOR resource "docker_image" "minecraft" { name =

    "hashicraft/minecraft:${var.version}" } resource "docker_container" "minecraft" { name = "minecraft" image = docker_image.minecraft.name }
  13. Command: terraform apply TERMINAL Apply complete! Resources: 1 added, 0

    changed, 0 destroyed. The state of your infrastructure has been saved to the path below. This state is required to modify and destroy your infrastructure, so keep it safe. To inspect the complete state use the `terraform show` command. State path: terraform.tfstate