How to use environment variables in docker compose

后端 未结 13 743
孤街浪徒
孤街浪徒 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:06

    It seems that docker-compose has native support now for default environment variables in file.

    all you need to do is declare your variables in a file named .env and they will be available in docker-compose.yml.

    For example, for .env file with contents:

    MY_SECRET_KEY=SOME_SECRET
    IMAGE_NAME=docker_image
    

    You could access your variable inside docker-compose.yml or forward them into the container:

    my-service:
      image: ${IMAGE_NAME}
      environment:
        MY_SECRET_KEY: ${MY_SECRET_KEY}
    

提交回复
热议问题