gpt4 book ai didi

dockerfile - 将变量从 Github Action 传递到 Docker 镜像构建

转载 作者:行者123 更新时间:2023-12-04 13:10:35 27 4
gpt4 key购买 nike

我一直致力于建立一个 Github Actions 工作流程来构建一个 docker 镜像。我需要将环境变量传递到图像中,以便我的 Django 项目能够正确运行。不幸的是,当我构建图像时,它没有收到变量的值。
我的工作流文件的相关部分:

     - name: Build, tag, and push image to AWS ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
aws_ses_access_key_id: ${{ secrets.AWS_SES_ACCESS_KEY_ID }}
aws_ses_secret_access_key: ${{ secrets.AWS_SES_SECRET_ACCESS_KEY }}
DATABASE_ENGINE: ${{ secrets.DATABASE_ENGINE }}
db_host: ${{ secrets.DB_HOST }}
db_password: ${{ secrets.DB_PASSWORD }}
db_port: ${{ secrets.DB_PORT }}
db_username: ${{ secrets.DB_USERNAME }}
django_secret_key: ${{ secrets.DJANGO_SECRET_KEY }}
fcm_server_key: ${{ secrets.FCM_SERVER_KEY }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_ENV
在我的 Dockerfile 中,我添加了以下内容:
ENV aws_ses_access_key_id=$aws_ses_access_key_id aws_ses_secret_access_key=$aws_ses_secret_access_key DATABASE_ENGINE=$DATABASE_ENGINE db_host=$db_host db_password=$db_password db_port=$db_port db_username=$db_username django_secret_key=$django_secret_key fcm_server_key=$fcm_server_key
没有任何变量通过。我试过使用 $variable_name 和 ${variable_name} 没有运气。我究竟做错了什么?

最佳答案

ENV 的值中使用美元替代Dockerfile 中的指令不会扩展为 docker build 所在主机的环境变量。被调用,但被替换为 Docker ARG值,您可以通过 --build-arg ARG_NAME=ARG_VALUE 传递docker build 的命令行参数命令,然后您可以替换 ARG_NAME 的值如 $ARG_NAMEARG_VALUE在您的 ENV操作说明。
见:https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg

You can use ENV instructions in a Dockerfile to define variable values. These values persist in the built image. However, often persistence is not what you want. Users want to specify variables differently depending on which host they build an image on.A good example is http_proxy or source versions for pulling intermediate files. The ARG instruction lets Dockerfile authors define values that users can set at build-time using the --build-arg flag


This flag allows you to pass the build-time variables that are accessed like regular environment variables in the RUN instruction of the Dockerfile. Also, these values don’t persist in the intermediate or final images like ENV values do. You must add --build-arg for each build argument.

关于dockerfile - 将变量从 Github Action 传递到 Docker 镜像构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65814953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com