Slide 1

Slide 1 text

Copyright © 2020 HashiCorp Understanding the Docker Provider for Terraform

Slide 2

Slide 2 text

Kerim Satirli (He/Him) Developer Advocate at HashiCorp

Slide 3

Slide 3 text

@ksatirli on GitHub and Twitter Developer Advocate at HashiCorp

Slide 4

Slide 4 text

Agenda Introducing Terraform basic concepts Managing Docker with Terraform provisioning Images, Containers and Networks Building out expanding your knowledge

Slide 5

Slide 5 text

Introducing Terraform

Slide 6

Slide 6 text

Terraform 125+ Official Providers Azure, GCP, Docker, etc. 175+ Community Providers Auth0, Sentry, Unifi, etc.

Slide 7

Slide 7 text

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 }

Slide 8

Slide 8 text

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" } }

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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" }

Slide 11

Slide 11 text

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" }

Slide 12

Slide 12 text

Command: terraform plan TERMINAL > terraform plan -out="docker.tfplan"

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

Command: terraform apply TERMINAL > terraform apply "docker.tfplan"

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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"

Slide 18

Slide 18 text

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"

Slide 19

Slide 19 text

Docker Container CODE EDITOR resource "docker_image" "minecraft" { name = "hashicraft/minecraft:${var.version}" } resource "docker_container" "minecraft" { name = "minecraft" image = docker_image.minecraft.name }

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Building out

Slide 22

Slide 22 text

Command: terraform fmt TERMINAL > terraform fmt -diff -check -recursive container.tf image.tf network.tf volume.tf

Slide 23

Slide 23 text

Command: terraform validate TERMINAL > terraform validate Success! The configuration is valid.

Slide 24

Slide 24 text

Java in Docker Switches and ores!

Slide 25

Slide 25 text

Review ▪ Providers ▪ Lifecycle ▪ Demo

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Thank You kerim@hashicorp.com