How to use environment variables in docker compose

后端 未结 13 768
孤街浪徒
孤街浪徒 2020-11-28 01:02

I would like to be able to use env variables inside docker-compose.yml, with values passed in at the time of docker-compose up. This is the example.

I am

13条回答
  •  伪装坚强ぢ
    2020-11-28 01:08

    The best way is to specify environment variables outside the docker-compose.yml file. You can use env_file setting, and define your environment file within the same line. Then doing a docker-compose up again should recreate the containers with the new environment variables.

    Here is how my docker-compose.yml looks like:

    services:
      web:
        env_file: variables.env
    

    Note: docker-compose expects each line in an env file to be in VAR=VAL format. Avoid using export inside the .env file. Also, the .env file should be placed in the folder where the docker-compose command is executed.

提交回复
热议问题