I have this exercise to do, however i worked out the docker image and container . . .

Ali Al Haj:
i have this exercise to do, however i worked out the docker image and container to run successfully, for the git commit to be added as a tag for the docker image i did below workaround, but when issuing the terraform apply command it’s still asking for the commit message even though it should take the value from the output block which takes it from the data block

The exercise:

Create Terraform that will run the container:
• It must expose port 8080.
• If using the local docker provider, use a separate bridge network labeled as “test”.
• The docker image must use the git commit as the tag.

My code:

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {
  host = "npipe:////.//pipe//docker_engine"
  # this host is used to run terraform on windows
  # in case the file needs to run on ubuntu it should have this host instead: "unix:///var/run/docker.sock"
}

variable "git_commit_message" {
  type = string
}

# Declare a data source to retrieve the Git commit message
data "external" "git_commit_message" {
  program = ["bash", "-c", "git log -1 --pretty=%B"]
}

# Set the value of the "git_commit_message" variable using the output of the data source
output "git_commit_message" {
  value = data.external.git_commit_message.result
}

resource "docker_image" "tidal" {
  depends_on = [data.external.git_commit_message]
  name        = "tidal:${var.git_commit_message}"
  keep_locally = false

  build {
    path = "."
  }
}

resource "docker_network" "private_network" {
  name  = "my_network"
  labels {
    label = "test"
    value = "test"
  }
}

resource "docker_container" "tidal" {
  image       = docker_image.tidal.name
  name        = "tidal"
  networks_advanced {
    name   = docker_network.private_network.name
    aliases = ["docknet"]
  }
  ports {
    internal = 8080
    external = 8080
  }
}

Cai Walkowiak:
As context:
The output value output.git_commit_message.value you are creating with the data.external.git_commit_message will give a result on each run, or if you issue terraform output
This can be referenced by outside programs - but I think what you are looking for is an internal reference to tag the docker image you are creating.

Your code block:

resource "docker_image" "tidal" {
  depends_on = [data.external.git_commit_message]
  name        = "tidal:${var.git_commit_message}"
  keep_locally = false

  build {
    path = "."
  }
}

Does not have the argument:

    tag  = ["zoo:develop"]

under the build {} block

  build {
    path = "."
    tag = data.external.git_commit_message.result
  }

That may not be exactly what you are looking for - but from your description that is what I am reading.

I also don’t know what the “This workaround means” - if it is from a lab it’s probably looking for something specific and Terraform is a “everything’s a nail” tool and can do things 10 different ways :smile:

Anyway - long time Terraform user, but new here! Have fun on your Terraform journey!

Ali Al Haj:
thanks for your reply Cai. I managed it to work ultimately

Cai Walkowiak:
:+1: Great to hear!