How to use environment variables in docker compose

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

    add env to .env file

    Such as

    VERSION=1.0.0
    

    then save it to deploy.sh

    INPUTFILE=docker-compose.yml
    RESULT_NAME=docker-compose.product.yml
    NAME=test
    
    prepare() {
            local inFile=$(pwd)/$INPUTFILE
            local outFile=$(pwd)/$RESULT_NAME
            cp $inFile $outFile
            while read -r line; do
                OLD_IFS="$IFS"
                IFS="="
                pair=($line)
                IFS="$OLD_IFS"
                   sed -i -e "s/\${${pair[0]}}/${pair[1]}/g" $outFile
                done <.env
         }
           
    deploy() {
            docker stack deploy -c $outFile $NAME
    }
    
            
    prepare
    deploy
        
    
    0 讨论(0)
提交回复
热议问题