将环境变量传递到通过Compose Yaml构建的Docker文件中 [英] Passing in environment variables into docker file that is build via a compose yaml

查看:157
本文介绍了将环境变量传递到通过Compose Yaml构建的Docker文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过运行docker-compose build命令通过我的docker compose文件调用一个docker文件.我在.env文件中定义了我的环境变量.当我执行构建时,在我的docker文件中,除此行外,其他所有东西都有效

I am calling a docker file thru my docker compose file by running command docker-compose build. I have my environment variables defined in a .env file. When I do the build, in my docker file, everything works except this line

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

但是,如果我在容器加载(docker-compose up)期间回显$ {USER_NAME},它将能够正确打印出该变量.

However if I echo ${USER_NAME} during container load (docker-compose up) it is able to print out that variable correctly.

ENTRYPOINT  echo ${USER_NAME}//this works

在docker文件中的run命令中传递环境变量的正确方法是什么?

更新:这是文件的精简版本

Update: Here is a stripped down version of the files

yaml文件

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

环境文件

USER_NAME="My test user"

Docker文件:

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

推荐答案

.env适用于docker-compose.yml.假设您是从compose内部构建Dockerfile(例如, docker-compose build ),则可以将 ARG 从compose传递到构建中,以为build <提供此变量代码>运行:

The .env applies to the docker-compose.yml. Assuming you are building a Dockerfile from within your compose (e.g. docker-compose build), then you can pass an ARG from compose into the build to provide this variable for build RUN:

docker-compose.yml:

docker-compose.yml:

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

Dockerfile:

Dockerfile:

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


这是我的实验室中的一个示例:


Here's an example of this in my lab:

$ 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

这篇关于将环境变量传递到通过Compose Yaml构建的Docker文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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