简体   繁体   English

如何在 Dockerfile 中访问云运行环境变量

[英]How to access cloud run environment variables in Dockerfile

I have built a containerised python application which runs without issue locally using a .env file and and a docker-compose.yml file compiled with compose build.我已经构建了一个容器化的 python 应用程序,它使用.env文件和一个用compose build编译的 docker-compose.yml文件在本地运行没有问题

I am then able to use variables within the Dockerfile like this.然后我可以像这样在 Dockerfile 中使用变量。

ARG APP_USR
ENV APP_USR ${APP_USR}

ARG APP_PASS
ENV APP_PASS ${APP__PASS}

RUN pip install https://${APP_USR}:${APP_PASS}@github.org/*****/master.zip

I am deploying to cloud run via a synced bitbucket repository, and have defined under "REVISIONS" > "SECRETS AND VARIABLES" ,(as described here: https://cloud.google.com/run/docs/configuring/environment-variables ) but I can not work out how to access these variables in the Dockerfile during build.我正在通过同步的 bitbucket 存储库部署到云运行,并已在"REVISIONS" > "SECRETS AND VARIABLES"下定义,(如此处所述: https : //cloud.google.com/run/docs/configuring/environment-variables ) 但我无法弄清楚如何在构建期间访问 Dockerfile 中的这些变量。

As I understand it, I need to create a cloudbuild.yaml file to define the variables, but I haven't been able to find a clear example of how to set this up using the Environment variables defined in cloud run.据我了解,我需要创建一个 cloudbuild.yaml 文件来定义变量,但是我无法找到一个清晰的示例来说明如何使用云运行中定义的环境变量进行设置。

You have several way to achieve that.您有几种方法可以实现这一目标。

You can, indeed, create your container with your .env in it.实际上,您可以在其中创建包含 .env 的容器。 But it's not a good practice because your .env can contain secret (API Key, database password,...) and because your container is tight to an environment但这不是一个好习惯,因为您的 .env 可以包含秘密(API 密钥、数据库密码,...),并且因为您的容器与环境紧密相关

The other solution is to deploy your container on Cloud Run (not a docker compose, it doesn't work on Cloud Run), and add the environment variable with the revision .另一种解决方案是在 Cloud Run 上部署您的容器(不是 docker compose,它在 Cloud Run 上不起作用),并添加带有 revision 的环境变量 use, for example, --set-env-vars=KEY1=Value1 format to achieve that.例如,使用--set-env-vars=KEY1=Value1格式来实现这一点。

If you have secrets, you can store them in secret manager and load it as env var at runtime, or as volume如果您有秘密,您可以将它们存储在秘密管理器中,并在运行时将其加载为 env var 或卷

The last solution, if you can specify where your container will get the .env file in your file tree (I'm not expert in Python to help you on that), you can use this trick that I described in this article .最后一个解决方案,如果您可以指定容器在文件树中获取 .env 文件的位置(我不是 Python 专家,无法帮助您),您可以使用我在本文中描述的这个技巧。 It's perfectly designed for configuration file, it's stored natively in Secret manager and therefore protect your secret automatically.它是为配置文件完美设计的,它本地存储在 Secret manager 中,因此可以自动保护您的秘密。

My understanding is that it is not possible to directly use a Cloud Run revision's environment variables in the Dockerfile because the build is managed by Cloud Build, which doesn't know about Cloud Run revision before the deployment.我的理解是,无法直接在 Dockerfile 中使用 Cloud Run 修订版的环境变量,因为构建是由 Cloud Build 管理的,它在部署之前不知道 Cloud Run 修订版。

But I was able to use Secret Manager's secrets in the Dockerfile.但我能够在 Dockerfile 中使用 Secret Manager 的秘密

Sources:资料来源:

Quick summary:快速总结:

In your case, for APP_USR and APP_PASS :在您的情况下,对于APP_USRAPP_PASS

  1. Grant the Secret Manager Secret Accessor (roles/secretmanager.secretAccessor) IAM role for the secret to the Cloud Build service account (see first source).将 Secret Manager Secret Accessor (roles/secretmanager.secretAccessor) IAM 角色授予 Cloud Build 服务帐号(参见第一个来源)。

  2. Add an availableSecrets block at the end of the cloudbuild.yaml file (out of the steps block):cloudbuild.yaml文件的末尾添加一个availableSecrets块(在steps块之外):

availableSecrets:
  secretManager:
  - versionName: <APP_USR_SECRET_RESOURCE_ID_WITH_VERSION>
    env: 'APP_USR'
  - versionName: <APP_PASS_SECRET_RESOURCE_ID_WITH_VERSION>
    env: 'APP_PASS'
  1. Pass the secrets to your build step (depends on how you summon docker build , Google's documentation uses 'bash', I use Docker directly):将秘密传递给您的构建步骤(取决于您如何调用 docker docker build ,Google 的文档使用“bash”,我直接使用 Docker):
  - id: Build
    name: gcr.io/cloud-builders/docker
    args:
      - build
      - '-f=Dockerfile'
      - '.'

      # Add these two `--build-arg` params:

      - '--build-arg'
      - 'APP_USR=$$APP_USR'

      - '--build-arg'
      - 'APP_PASS=$$APP_PASS'

    secretEnv: ['APP_USR', 'APP_PASS'] # <=== add this line
  1. Use these secrets as standard environment variables in your Dockerfile :Dockerfile使用这些机密作为标准环境变量:
ARG APP_USR
ENV APP_USR $APP_USR

ARG APP_PASS
ENV APP_PASS $APP_PASS

RUN pip install https://$APP_USR:$APP_PASS@github.org/*****/master.zip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM