How to use environment variables in docker compose

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

    The following is applicable for docker-compose 3.x Set environment variables inside the container

    method - 1 Straight method

    web:
      environment:
        - DEBUG=1
          POSTGRES_PASSWORD: 'postgres'
          POSTGRES_USER: 'postgres'
    

    method - 2 The “.env” file

    Create a .env file in the same location as the docker-compose.yml

    $ cat .env
    TAG=v1.5
    POSTGRES_PASSWORD: 'postgres'
    

    and your compose file will be like

    $ cat docker-compose.yml
    version: '3'
    services:
      web:
        image: "webapp:${TAG}"
        postgres_password: "${POSTGRES_PASSWORD}"
    

    source

提交回复
热议问题