简体   繁体   中英

Azure App Service - resolving environment variables in docker-compose yml

I have a multicontainer application that I would like to deploy and run within an Azure App Service. The compose file I have, docker-compose.prod.yml, contains environment variables (as shown in the below code).

When I run build the compose file locally, these variables are resolved during the build process by referencing an environment file (env.) located in the same directory:

docker-compose -f docker-compose.prod.yml build push

I know that it is possible to build a DevOps pipeline (as described here ), which will insert environment variables during the build process, similar to how I build it locally.

However, I wanted to know if it is possible to either

1.) configure environment variables via the Azure Portal web interface (as you can do with application settings), which the docker compose file can then reference to resolve these variables on startup

or

2.) somehow upload the env. file I use locally which can then be used to resolve the variables on create or startup.

Contents of docker-compose.prod.yml
--------------------------------------------
version: '3.7'

 

services:
  app1:
    build: ./app1
    command : python3 manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000
    image: "${AZ_CONTAINER_REGISTRY}/${APP1_IMAGE}:${APP1_VERSION}"
  app2:
    build: ./app2
    command: python app.py
    ports:
      - 8081:8080
    image: "${AZ_CONTAINER_REGISTRY}/${APP2_IMAGE}:${APP2_VERSION}"
    restart: always

 


Contents of env. file
--------------------------------------------
AZ_CONTAINER_REGISTRY=sample.azurecr.io

 

APP1_IMAGE=app1test
APP1_VERSION=1.0

 

APP2_IMAGE=app2test
APP2_VERSION=2.0

Any feedback is appreciated, thanks

1.) configure environment variables via the Azure Portal web interface (as you can do with application settings), which the docker compose file can then reference to resolve these variables on startup

According to my knowledge, the Azure Web App does not support that uses variables instead of the real image currently. Only some variables that Azure gives can be used in the docker-compose file, such as the variable WEBAPP_STORAGE_HOME for persistent storage.

2.) somehow upload the env. file I use locally which can then be used to resolve the variables on create or startup

And it also does not support the env. file, all the docker commands are executed by Azure in the backend. Otherwise, the Azure Web App also does not support the build option in the docker-compopse file, see the supported options .

I was struggling with the exact same question. After a lot of searching I found this post describing a nice little hack .

Add this to your init/startup script:

# Get environment variables to show up in SSH session
eval $(printenv | awk -F= '{print "export " "\""$1"\"""=""\""$2"\"" }' >> /etc/profile)

I can now run my management commands via SSH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM