developer747
developer747

Reputation: 15928

Passing in environment variables into docker file that is build via a compose yaml

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}

It fails with the message

←[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

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

What is the right way to pass in an environment variable in a run command in the docker file?

Update: Here is a stripped down version of the files

yaml file

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

Env file

USER_NAME="My test user"

Docker file:

FROM xxx

ARG USER_NAME 

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

Commands Build:

docker-compose build git

Run:

docker-compose up git

The build fails with the error

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

Upvotes: 0

Views: 5128

Answers (2)

BMitch
BMitch

Reputation: 263489

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:

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

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

Upvotes: 4

BMW
BMW

Reputation: 45223

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

refer: Environment replacement¶

Upvotes: 0

Related Questions