Docker上的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
来构建:
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.
Docker版本是1.12.6
,它不支持使用--build-arg
传递参数.
Docker version is 1.12.6
which doesn't support passing arguments with --build-arg
.
我尝试按照此处所述使用.env
文件:
I tried using .env
file as described here:
cat .env
REP_USER=myusername
REP_PASS=mypassword
然后我打电话给docker-compose config
,它返回了:
I then called docker-compose config
which returned:
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.
我还尝试了 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
,它返回了:
I then called docker-compose config
which returned:
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上的Dockerfile传递环境组成构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!