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