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
PRO

May 27, 2020
Tweet

More Decks by Kerim Satirli

Other Decks in Programming

Transcript

  1. Copyright © 2020 HashiCorp Understanding the Docker Provider for Terraform

  2. Kerim Satirli (He/Him) Developer Advocate at HashiCorp

  3. @ksatirli on GitHub and Twitter Developer Advocate at HashiCorp

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

    Images, Containers and Networks Building out expanding your knowledge
  5. Introducing Terraform

  6. Terraform 125+ Official Providers Azure, GCP, Docker, etc. 175+ Community

    Providers Auth0, Sentry, Unifi, etc.
  7. 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 }
  8. 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" } }
  9. 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.
  10. 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" }
  11. 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" }
  12. Command: terraform plan TERMINAL > terraform plan -out="docker.tfplan"

  13. 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.
  14. Command: terraform apply TERMINAL > terraform apply "docker.tfplan"

  15. 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
  16. 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
  17. 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"
  18. 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"
  19. Docker Container CODE EDITOR resource "docker_image" "minecraft" { name =

    "hashicraft/minecraft:${var.version}" } resource "docker_container" "minecraft" { name = "minecraft" image = docker_image.minecraft.name }
  20. 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
  21. Building out

  22. Command: terraform fmt TERMINAL > terraform fmt -diff -check -recursive

    container.tf image.tf network.tf volume.tf
  23. Command: terraform validate TERMINAL > terraform validate Success! The configuration

    is valid.
  24. Java in Docker Switches and ores!

  25. Review ▪ Providers ▪ Lifecycle ▪ Demo

  26. Materials ▪ slides: hashi.co/tf-basics-for-docker ▪ code: hashi.co/tf-basics-for-docker-code ▪ guides: hashi.co/tf-learn

    ▪ forums: hashi.co/tf-providers-forum
  27. None
  28. Thank You kerim@hashicorp.com