【问题标题】:Can you run Docker containers in GCP via Terraform?你可以通过 Terraform 在 GCP 中运行 Docker 容器吗?
【发布时间】:2020-06-25 00:50:28
【问题描述】:

我创建了一个 Docker 映像,我想使用 Terraform 在 GCP 中运行它。我已经像这样标记并将图像推送到 GCR:

docker tag carlspring/hello-spring-boot:1.0 eu.gcr.io/${PROJECT_ID}/carlspring/hello-spring-boot:1.0
docker push eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0

我有以下代码:

provider "google" {
  // Set this to CREDENTIALS
  credentials = file("credentials.json")

  // Set this to PROJECT_ID
  project = "carlspring"
  region  = "europe-west2"
  zone    = "europe-west2-a"
}

resource "google_compute_network" "vpc_network" {
  name = "carlspring-terraform-network"
}


resource "google_compute_instance" "docker" {
  count        = 1
  name         = "tf-docker-${count.index}"
  machine_type = "f1-micro"
  zone         = var.zone
  tags         = ["docker-node"]

  boot_disk {
    initialize_params {
      image = "carlspring/hello-spring-boot"
    }
  }
}

做完之后:

terraform init
terraform plan
terraform apply

我明白了:

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

google_compute_instance.docker[0]: Creating...

Error: Error resolving image name 'carlspring/hello-spring-boot': Could not find image or family carlspring/hello-spring-boot

  on main.tf line 18, in resource "google_compute_instance" "docker":
  18: resource "google_compute_instance" "docker" {

我在网上看到的示例要么使用 K8s,要么启动运行 Linux 的 VM 映像,其中安装了 Docker 并且正在启动映像。我不能简单地使用我自己的容器来启动实例吗?

【问题讨论】:

  • 您正在尝试为启动磁盘映像指定一个容器。您需要指定容器操作系统启动映像并在操作系统内运行容器。这个例子会对你有所帮助:github.com/terraform-google-modules/…

标签: docker google-cloud-platform terraform google-container-os


【解决方案1】:

google_compute_instance 需要一个 VM 映像,而不是 Docker 映像。如果您想将 Docker 映像部署到 GCP,最简单的选择是 Cloud Run。要将其与 Terraform 一起使用,您需要 cloud_run_service

例如:

resource "google_cloud_run_service" "default" {
  name     = "cloudrun-srv"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0"
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
}

请注意,我使用的是 eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0 而不是 carlspring/hello-spring-boot。您必须使用完全限定名称作为指向 Docker Hub 的短点,在该位置找不到您的映像。

【讨论】:

  • 谢谢!有了这个我得到Error: Error waiting to create Service: resource is in failed state "Ready:False", message: Cloud Run error: Invalid argument error. Invalid ENTRYPOINT. [name: "eu.gcr.io/carlspring/carlspring/hello-spring-boot@sha256:4ca4129f5d19f78a46c64ab65b501497ddc48491e86e17b36615fb4a338da865" error: "Invalid command \"/bin/sh\": file not found" ]. 。有什么建议吗?
  • 听起来您的图像已损坏并且入口点错误。您可以修复它或使用command 覆盖它。
  • 我的Dockerfile 末尾有以下内容:ENTRYPOINT java -jar /java/hello-spring-boot*1.0-SNAPSHOT-spring-boot.war。在 Docker 中本地运行它可以正常工作。在此之前和重新部署之前,我实际上也有这个 CMD。正确的command 是什么样的?
  • 如果你的图片中没有/bin/sh,你需要ENTRYPOINT ["java", "-jar", "/java/hello-sprint-boot*1.0-SNAPSHOT-sprint-boot.war"]。不过,您可能必须摆脱那个*
  • 感谢您的建议和帮助!
【解决方案2】:

Terraform 可用于使用 Docker Image 创建 GCP VM 实例。 这是一个例子:https://github.com/terraform-providers/terraform-provider-google/issues/1022#issuecomment-475383003

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    以下行表示图片不存在:

    Error: Error resolving image name 'carlspring/hello-spring-boot': Could not find image or family carlspring/hello-spring-boot
    

    您应该将图像标记为eu.gcr.io/carlspring/hello-spring-boot:1.0

    或者,将boot_disk 块中的图像引用更改为eu.gcr.io/carlspring/carlspring/hello-spring-boot:1.0

    【讨论】:

    • @Elgami:我已经用更多细节更新了我的问题。我标记它的方式是这样的:docker tag carlspring/hello-spring-boot:1.0 eu.gcr.io/${PROJECT_ID}/carlspring/hello-spring-boot:1.0。你说这不正确吗?
    • 感谢您的建议和帮助!
    【解决方案4】:

    您可以使用 GCE 中的虚拟机来执行此操作,该虚拟机的操作系统基于 Google 提供的容器操作系统映像。然后你可以use this terraform module 方便容器镜像的获取和运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-24
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2018-10-25
      • 2019-06-09
      相关资源
      最近更新 更多