Docker上的Dockerfile传递环境组成构建 [英] Dockerfile pass environments on docker compose build

查看:98
本文介绍了Docker上的Dockerfile传递环境组成构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个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屋!

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