You need to enable JavaScript to run this app. 非Cloud Build部署的Cloud Run容器如何从Google Secret Manager加载环境变量
优惠活动
大模型
产品
解决方案
定价
更多

非Cloud Build部署的Cloud Run容器如何从Google Secret Manager加载环境变量

项目基础信息
  • 基于Node + NestJS开发应用,计划打包为Docker容器部署至Google Cloud Run平台
  • 所有密钥统一存储在Google Secret Manager,按dev/staging/prod环境划分独立GCP项目,核心目标是将密钥顺畅供给云上运行的容器
  • 现有部署流程通过触发yarn docker:auth命令执行一系列bash脚本完成全链路操作

当前部署执行命令

docker build -t gcr.io/my_project_id_dev/auth-service -f .docker/auth.DockerFile . &&
gcloud auth activate-service-account abc@my_project_id_dev.iam.gserviceaccount.com --key-file=gcloud-sa.json &&
gcloud auth configure-docker &&
docker push gcr.io/my_project_id_dev/auth-service &&
gcloud beta run services replace .gcp/cloud_run/auth.yaml &&
gcloud run deploy auth ... --allow-unauthenticated  --platform managed --region europe-west2

现存问题

  • 部署命令参数失效:最后一条gcloud run deploy传入的配置参数未生效,每次执行命令都需要手动选择平台和区域
  • YAML配置语法报错:参考Cloud Build文档示例尝试在auth.yaml中添加secrets相关配置时,直接将如下内容写在yaml文件最外层、无所属父级节点的写法持续触发语法错误:
    availableSecrets:
      secretManager:
        - versionName: projects/my_project_id/secrets/mongo_uri/versions/latest
        env: 'mongo_uri'
    

待确认核心疑问

  • 上述场景下,完全通过YAML配置实现Secret向容器的注入是否可行?
  • 目前已在Node应用中新增启动逻辑,通过npm包@google-cloud/secret-manager将密钥加载到环境变量,本地使用默认凭证运行无异常,但部署到Cloud Run时存在两个待明确的问题:
    • Cloud Run上运行的Docker容器是否具备默认凭证?
    • 若不存在默认凭证,最优的凭证注入方式是什么?明确禁止将服务账号密钥文件打包进镜像的不安全实践

现有Cloud Run服务YAML配置

为容器指定关联服务账号的配置内容如下:

spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/maxScale: '2'
        run.googleapis.com/client-name: cloud-console
        run.googleapis.com/sandbox: gvisor
    spec:
      containerConcurrency: 2
      containers:
      - image:  gcr.io/my_project_id/auth-service
        ports:
        - containerPort: 3000
        resources:
          limits:
            cpu: 1000m
            memory: 512Mi
      serviceAccountName: abc@my_project_id.iam.gserviceaccount.com
      timeoutSeconds: 300
  • 完成上述配置后仍触发通用启动报错,提示容器启动失败、未监听PORT环境变量定义的端口,日志无详细报错信息
  • 本地测试时,带有密钥加载逻辑的容器因未注入服务账号无法启动,去掉该段密钥加载代码后本地容器可正常运行

核心诉求

将Secret Manager中的密钥正确注入到Cloud Run运行的容器环境中


内容的提问来源于stack exchange,提问作者SebastianG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.31 13:06:05