如何通过 Azure DevOps 在 Dockerfile 中设置环境变量 [英] How to set environment variables in Dockerfile via Azure DevOps

查看:37
本文介绍了如何通过 Azure DevOps 在 Dockerfile 中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目 Docker 文件中,我有一些环境变量,如下所示:

In my projects Docker file I have some environment variables, like this:

ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=Password
ENV MSSQL_PID=Developer
ENV MSSQL_TCP_PORT=1433 

我想在此处将密码作为在我的管道中设置的环境变量传递.

And I would like to pass the password here as an environment variable set in my pipeline.

在 Azure DevOps 中,我有两个管道.一种用于构建解决方案,一种用于构建 docker 镜像并将其推送到 DockerHub.在这两个管道中都有设置变量的选项:我已经在两个管道中设置了密码,并在 Dockerfile 中编辑了我的密码,如下所示:

In Azure DevOps I have two pipelines. One for building the solution and one for building and pushing docker images to DockerHub. There are options to set variables in both these pipelines: I have set the password in both pipelines and edited my password in the Dockerfile to look like this:

ENV SA_PASSWORD=$(SA_PASSWORD)

但这似乎不起作用.将环境变量从 Azure DevOps 传递到 Docker 映像的正确方法是什么?

But that does not seem to be working. What is the correct way of passing environment variables from Azure DevOps into a Docker image?

另外,这是一种安全的传递秘密的方式吗?有什么办法可以从 Docker 镜像中读取机密吗?

Also, is this a safe way of passing secrets? Is there any way someone could read secrets from a Docker image?

谢谢!

推荐答案

您可以设置 ARG var_name 并将 ENV 引用到 ARG 变量.然后你可以在 docker 构建镜像时替换这些变量 $ docker build --build-arg var_name=$(VARIABLE_NAME)

You can set an ARG var_name and reference ENV to the ARG variables. Then you can replace those variables when docker build the image $ docker build --build-arg var_name=$(VARIABLE_NAME)

例如在 dockerfile 中添加 ARG,并让 ENV 变量引用它:

For example the add ARG in dockerfile, and have the ENV variable refer to it:

ARG SECRET
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=$SECRET
ENV MSSQL_PID=Developer
ENV MSSQL_TCP_PORT=1433 

您可以单独使用停靠构建任务和停靠推送任务,因为 buildandpush 命令不能接受参数.并在您的管道中设置一个变量 SECRET.

You can use dock build task and dock push task separately, as buildandpush command cannot accept arguments. And set a variable SECRET in your pipeline.

设置Build Arguments SECRET= $(SECRET) 替换ARG SECRET

The set the Build Arguments SECRET= $(SECRET) to replace the ARG SECRET

你也可以参考一个类似的线程.

You can also refer to a similar thread.

这篇关于如何通过 Azure DevOps 在 Dockerfile 中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆