mzafer
mzafer

Reputation: 851

Copy container image from one Google project to another using terraform

I have an image in the container registry of say "Project A" and want to copy it over to the container registry of "Project B". How do I do it using terraform ?

Upvotes: 1

Views: 1638

Answers (1)

Semafoor
Semafoor

Reputation: 2032

I don't think you should, but that does not mean you can't.

Don't Terraform is an excellent tool to manage infrastructure with; it's not great for interacting with data or application code. The line between these two can be hard to draw, especially with GCP's high-level services like AppEngine, Cloud Run, ..., but imo, where a container registry can be treated as infrastructure, a container image should be treated as data (or code), and not be managed in Terraform. Don't do it!

But I really want to... Hey, who am I to judge? Maybe there's something about your particular problem that I don't know about. There's no GCP resources for copying or interacting with images, but you can interact with GCR as generic container registries. You can use the community module neomantra/mirror/docker. Its sources are pretty easy to read and adapt; it uses the docker_image resource to pull the remote image to your local machine, followed by a well-timed local-exec to docker push the image to its destination.

Example usage:

module "docker-mirror-vault" {
  source        = "github.com/neomantra/terraform-docker-mirror?ref=v0.4.0"
  image_name    = "myexample"
  image_tag     = "latest"
  source_prefix = "us.gcr.io/my-one-gcp-project"
  dest_prefix   = "us.gcr.io/my-other-gcp-project"
}

You can optionally use the data resource google_container_registry_image for constructing the handles to the images in Container Registry.

Fyi, if you're migrating, consider migrating to Artifact Registry, as Google is deprecating the Container Registry in favor of the Artifact Registry.

Upvotes: 2

Related Questions