Use docker-compose env variable in Dockerbuild file

后端 未结 3 628
粉色の甜心
粉色の甜心 2020-12-24 12:40

Having the following docker-compose file:

db:
    build: .
    environment:
        - MYSQL_ROOT_PASSWORD=password
        - ENV=test
    env_file: .env
         


        
3条回答
  •  猫巷女王i
    2020-12-24 13:16

    Although this question was asked long ago, there is an answer to a similar question here: Pass environment variables from docker-compose to container at build stage

    Basically, to use variables at the container's build time one has to define the variable in docker-compose.yml:

    build:
      context: .
      args:
        MYSQL_ROOT_PASSWORD: password
        ENV: test
    

    and then reference it in the Dockerfile using ARG:

    ARG MYSQL_ROOT_PASSWORD
    ARG ENV
    ADD ${ENV}/data.xml /data/
    

    Concerning environment variables defined in an *.env file, I believe that they can't be passed to the container at build time.

提交回复
热议问题