docker compose build 上的 Dockerfile 传递环境 [英] Dockerfile pass environments on docker compose build
问题描述
我写了一个 Dockerfile
它使用两个参数:
I have written a Dockerfile
which uses two arguments:
FROM jessie
MAINTAINER Zeinab Abbasimazar
#Build Arguments
ARG REP_USER
ARG REP_PASS
# Build
RUN echo 'REP_USER:'$REP_USER', REP_PASS:'$REP_PASS
我写了一个 docker-compose.yml
用于构建:
I wrote a docker-compose.yml
for build:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
args:
REP_USER: $REP_USER
REP_PASS: $REP_PASS
我不想直接在 compose 文件中定义这些参数,所以我尝试在 docker compose build 期间发送它们:
I don't want to define these arguments directly in the compose file, so I tried to send them during docker compose build:
REP_USER=myusername REP_PASS=mypassword docker-compose build
这没有用.我更改了我的 Dockerfile
以使用这些参数作为环境变量;所以我删除了 ARG
行:
Which didn't work. I changed my Dockerfile
to use these arguments as environment variables; so I removed ARG
lines:
FROM jessie
MAINTAINER Zeinab Abbasimazar
# Build
RUN echo 'REP_USER:'$REP_USER', REP_PASS:'$REP_PASS
和docker-compose.yml
:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
然后运行REP_USER=myusername REP_PASS=mypassword docker-compose build
;还是没有结果.
And ran REP_USER=myusername REP_PASS=mypassword docker-compose build
; still no result.
我也尝试将这些信息保存到 env
文件中:
I also tried to save these information into an env
file:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
env_file:
- myenv.env
但似乎 env
文件在构建时没有影响;它们只是参与运行时.
But it seems env
files doesn't affect at build time; they are just take part into run time.
编辑 1:
Docker 版本是 1.12.6
,它不支持使用 --build-arg
传递参数.
Docker version is 1.12.6
which doesn't support passing arguments with --build-arg
.
编辑 2:
我尝试使用 .env
文件,如这里:
I tried using .env
file as described here:
cat .env
REP_USER=myusername
REP_PASS=mypassword
然后我调用 docker-compose config
返回:
networks: {}
services:
ui:
build:
args:
REP_PASS: mypassword
REP_USER: myusername
context: /home/zeinab/Workspace/ZiZi-Docker/Test/test-exec-1
dockerfile: Dockerfile
version: '2.0'
volumes: {}
这意味着这解决了我的问题.
Which means this resolved my issue.
编辑 3:
我还在我的文章中尝试了 docker-compose arg 文档的第三部分docker-compose.yml
文件:
I also tried third section of docker-compose arg documentation in my docker-compose.yml
file:
version: "2"
services:
ui:
build:
context: .
dockerfile: Dockerfile
args:
- REP_USER
- REP_PASS
并执行:
export REP_USER=myusername;export REP_PASS=mypassword;sudo docker-compose build --no-cache
仍然没有得到我想要的.
Still not getting what I wanted.
推荐答案
我终于找到了解决方案.我在问题中也提到过.我首先尝试失败,然后我发现我在命名 .env
文件时有一个错字;它是 .evn
.
I finally found the solution. I mentioned it in the question too. I first tried it with fail, then I found out that I had a typo naming .env
file; it was .evn
.
我尝试使用 .env
文件,如此处:
I tried using .env
file as described here:
cat .env
REP_USER=myusername
REP_PASS=mypassword
然后我调用 docker-compose config
返回:
networks: {}
services:
ui:
build:
args:
REP_PASS: mypassword
REP_USER: myusername
context: /home/zeinab/Workspace/ZiZi-Docker/Test/test-exec-1
dockerfile: Dockerfile
version: '2.0'
volumes: {}
这意味着这解决了我的问题.我应该提到 这个答案真的很有帮助.
Which means this resolved my issue. I should mention that this answer was really helpful.
这篇关于docker compose build 上的 Dockerfile 传递环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!