Dockerfile pass environments on docker compose bui

2020-04-06 15:47发布

问题:

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

I wrote a docker-compose.yml for build:

version: "2"
services:
  ui:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        REP_USER: $REP_USER
        REP_PASS: $REP_PASS

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

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

And docker-compose.yml:

version: "2"
  services:
    ui:
      build:
        context: .
        dockerfile: Dockerfile

And ran REP_USER=myusername REP_PASS=mypassword docker-compose build; still no result.

I also tried to save these information into an env file:

version: "2"
services:
  ui:
    build:
      context: .
      dockerfile: Dockerfile
    env_file:
      - myenv.env

But it seems env files doesn't affect at build time; they are just take part into run time.

EDIT 1:

Docker version is 1.12.6 which doesn't support passing arguments with --build-arg.

EDIT 2:

I tried using .env file as described here:

cat .env 
REP_USER=myusername
REP_PASS=mypassword

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.

EDIT 3:

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

And executed:

export REP_USER=myusername;export REP_PASS=mypassword;sudo docker-compose build --no-cache

Still not getting what I wanted.

回答1:

You can set build arguments with docker compose as described here:

docker-compose build [--build-arg key=val...]
docker-compose build --build-arg REP_USER=myusername --build-arg REP_PASS=mypassword

Btw, AFAIK build arguments are a compromise between usability and deterministic building. Docker aims to build in a deterministic fashion. That is, wherever you execute the build the produced image should be the same. Therefore, it appears logical that the client ignores the environment (variables) it is executed in.



回答2:

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.

I tried using .env file as described here:

cat .env 
REP_USER=myusername
REP_PASS=mypassword

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.



回答3:

The correct syntax for variable substitution in a docker-compose file is ${VARNAME}. Try with this one:

version: "2"
  services:
    ui:
      build:
        context: .
        dockerfile: Dockerfile
      args:
        REP_USER: ${REP_USER}
        REP_PASS: ${REP_PASS}