Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile pass environments on docker compose build

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.

like image 898
Zeinab Abbasimazar Avatar asked Aug 29 '17 07:08

Zeinab Abbasimazar


People also ask

Can I use environment variables in docker compose file?

Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.

How do I pass environment variables to docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...


2 Answers

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.

like image 188
fzgregor Avatar answered Oct 30 '22 18:10

fzgregor


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}
like image 39
whites11 Avatar answered Oct 30 '22 19:10

whites11