如何通过Azure DevOps在Dockerfile中设置环境变量 [英] How to set environment variables in Dockerfile via Azure DevOps
问题描述
在我的项目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.
设置构建参数SECRET= $(SECRET)
来替换ARG秘密
The set the Build Arguments SECRET= $(SECRET)
to replace the ARG SECRET
您还可以引用类似的 thread .
这篇关于如何通过Azure DevOps在Dockerfile中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!