Using local Docker Images in Terraform

Viewed 1624

I want to use a local Docker Image inside Terraform. More concrete, I have a Google Cloud Build Pipeline that builds a Docker Image that I want to deploy to GCP Cloud Run with Terraform to perform tests, before I push the Docker Image to the Google Container Registry.


First, I just tried to access the freshly build local Docker Image from Terraform, but that did not work.

cloudbuild-1.yaml

steps:
    - id: my-service 
      name: 'custom-terraform-docker-image'
      dir: 'my-service'
      entrypoint: '/bin/sh'
      args:
      - '-c'
      - |
        docker build -t my-service-image . --file prod.dockerfile && terraform init && terraform apply -auto-approve

and the corresponding terraform configuration

main-1.tf

resource "google_cloud_run_service" "my-service" {
  name     = "my-service-name"
  location = var.location

  template {
    spec {
      containers {
        image = "my-service-image"
        env {
                name = "ENVIRONMENT"
                value = "PROD"
        }
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
}

Another thing that I tried, was to use a local Docker Registry. I created this registry using docker run -d -p 5000:5000 --restart=always --name registry registry:2 and pushed my local Docker Image docker tag my-service-image:latest localhost:5000/my-service-image and docker push localhost:5000/my-service-image. This approach still does not work...

As you can see, I'm using a custom Docker Image in my Google Cloud Build Step that contains Terraform and Docker to be able to use them together in the same isolated environment. In the Terraform documentation I could not find any information on how to access local Docker Images and don't understand why especially the version using the local registry doesn't work. Any Ideas?

cloudbuild-2.yaml

steps:
    - id: my-service 
      name: 'custom-terraform-docker-image'
      dir: 'my-service'
      entrypoint: '/bin/sh'
      args:
      - '-c'
      - |
        docker build -t my-service-image . --file prod.dockerfile && docker run -d -p 5000:5000 --restart=always --name registry registry:2 && docker tag my-service-image:latest localhost:5000/my-service-image:latest && docker push localhost:5000/my-service-image:latest && terraform init && terraform apply -auto-approve

main-2.tf

resource "google_cloud_run_service" "my-service" {
  name     = "my-service-name"
  location = var.location

  template {
    spec {
      containers {
        image = "localhost:5000/my-service-image"
        env {
                name = "ENVIRONMENT"
                value = "PROD"
        }
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
}

Note: I found a way around it, by pushing the Docker Image Candidate to the Container Registry and use the pushed Image inside Terraform using gcr.io/{project_name}/image. But I want to avoid this step, since I prefer to only push Docker Images that have been tested to the Container Registry.

0 Answers
Related