2016-12-02 34 views
0

我通过运行命令docker-compose build来调用docker文件,通过我的docker撰写文件。我在.env文件中定义了我的环境变量。当我这样做的身材,在我的搬运工文件,一切正常,除了这行将环境变量传递到通过撰写yaml构建的docker文件

RUN git config --global user.email ${USER_NAME} 

它失败的消息

←[31mERROR←[0m: Service 'git' failed to build: The command '/bin/sh -c git confi 
g --global user.email ${USER_NAME}' returned a non-zero code: 1 

但是,如果我赞同集装箱时$ {USER_NAME}(泊坞窗,撰写它可以正确打印出这个变量。

ENTRYPOINT echo ${USER_NAME}//this works 

什么是在一个环境变量传递在泊坞窗文件运行命令的正确方法?

更新:这是文件的精简版

YAML文件

version: '2' 
services: 
    git: 
     build: 
      context: ./git 
      args: 
      - USER_NAME 
     env_file: 
     - ./common.env 

信封文件

USER_NAME="My test user" 

泊坞文件:

FROM xxx 

ARG USER_NAME 

RUN git config --global user.name ${USER_NAME} 
ENTRYPOINT git config --list 

命令 体形:

docker-compose build git 

运行:

docker-compose up git 

构建失败,错误

RUN git config --global user.name ${USER_NAME} 
---> Running in 7b67ddeae989 
←[31mERROR←[0m: Service 'git' failed to build: The command '/bin/sh -c git config --global user.name ${USER_NAME}' returned a non-zero code: 1 
+0

包含一个更完整的docker-compose.yml和Dockerfile示例可以更容易地提供帮助。 – BMitch

+0

您的'git'命令失败了,从提供的有限错误信息中,无法说明原因(是否安装了git)?尝试直接在容器中运行它,并确保它不会提示输入或提供不良的退出代码。您的'.env'文件内容也可能有助于调试。 – BMitch

+0

添加了.env内容。如果我硬编码它的用户名。在运行时直接读取环境也可以。 – developer747

回答

0
ENV USER_NAME developer747 
RUN git config --global user.email ${USER_NAME} 

参考:Environment replacement¶

+0

但我希望它从.env文件读取此环境。如果我在入口处阅读它,这将起作用。在您发布的示例中,它正在docker文件中进行硬编码,而不是从文件中读取。 – developer747

2

的.ENV AP使用docker-compose.yml。假设你是从你撰写内部建立一个Dockerfile(如docker-compose build),那么你可以通过从撰写ARG到构建,为构建RUN提供这个变量:

泊坞窗,compose.yml:

... 
build: 
    args: 
    USER_NAME: ${USER_NAME} 

Dockerfile:

... 
ARG USER_NAME=developer747 
RUN git config --global user.email ${USER_NAME} 

下面是这在我的实验室的例子:

$ cat docker-compose.build-arg.yml 
version: '2' 

services: 
    build-test: 
    build: 
     args: 
     USER_NAME: ${USER_NAME} 
     context: . 
     dockerfile: df.build-arg 
    image: test-build-args 

$ cat .env 
ENV=default 
USER_NAME=test2 

$ cat df.build-arg 
FROM busybox 

ARG USER_NAME=default 
RUN adduser --disabled-password ${USER_NAME} 
CMD tail -f /dev/null 

$ docker-compose -f docker-compose.build-arg.yml up --build -d 
Building build-test 
Step 1 : FROM busybox 
---> 2b8fd9751c4c 
Step 2 : ARG USER_NAME=default 
---> Using cache 
---> 9be5b562c784 
Step 3 : RUN adduser --disabled-password ${USER_NAME} 
---> Using cache 
---> bcbaf683e3cf 
Step 4 : CMD tail -f /dev/null 
---> Running in 66908e4f7a0c 
---> 06b9774253c2 
Removing intermediate container 66908e4f7a0c 
Successfully built 06b9774253c2 
Recreating test_build-test_1 

$ docker exec -it test_build-test_1 /bin/sh 
/# tail /etc/passwd 
root:x:0:0:root:/root:/bin/sh 
daemon:x:1:1:daemon:/usr/sbin:/bin/false 
bin:x:2:2:bin:/bin:/bin/false 
sys:x:3:3:sys:/dev:/bin/false 
sync:x:4:100:sync:/bin:/bin/sync 
mail:x:8:8:mail:/var/spool/mail:/bin/false 
www-data:x:33:33:www-data:/var/www:/bin/false 
operator:x:37:37:Operator:/var:/bin/false 
nobody:x:99:99:nobody:/home:/bin/false 
test2:x:1000:1000:Linux User,,,:/home/test2:/bin/sh 
/# exit 
+0

我不想在docker文件中硬编码值developer747。它应该来自.env文件。 – developer747

+0

那里没有硬编码,如果在Docker构建过程中无法传递arg,则Dockerfile中的值是默认值。选择一个合理的默认值,这对您的图像有意义。 – BMitch

+0

我试过了。它选择了开发者价值747。它没有选择.env文件中的内容。显然,在构建时间和运行时间期间有不同的方式来获取环境变量。我会多玩一点。 – developer747