简体   繁体   中英

Azure Web App in custom container, env variables not propagated from app service to container

So I've deployed my app to azure app services with custom docker container running on Alpine, but none of the configured environment variables are available in bash when I SSH to it. The application entry point itself sees all vars and app runs correctly. The info I was able to dig up in microsoft docs says that all config vars should be passed down, but somehow they are not... Any suggestions are welcome

Here is a post that resembles my issue, yet it doesn't offer any solution either.

here is my dockerfile


# pull official base image
FROM python:3.7-alpine AS build-python

# Copy over and install requirements files
RUN mkdir requirements

COPY requirements/production.txt /requirements/
COPY requirements/common.txt /requirements/
COPY ./requirements.txt /
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /wheels -r requirements.txt

FROM python:3.7-alpine

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0
ENV DJANGOENV staging

# install psycopg2
RUN apk update \
    && apk add --virtual build-deps pkgconfig python3-dev openssl-dev libxml2-dev libxslt-dev libffi-dev musl-dev make gcc \
    && apk add postgresql-dev \
    && pip install psycopg2

RUN apk add openssh \
     && echo "root:Docker!" | chpasswd
COPY sshd_config /etc/ssh/

RUN /usr/bin/ssh-keygen -A

EXPOSE 2222
EXPOSE 80
EXPOSE 8000

COPY --from=build-python /wheels /wheels
COPY --from=build-python requirements .
COPY --from=build-python requirements.txt .
RUN pip install --no-cache /wheels/*

RUN apk del build-deps

# set work directory
WORKDIR /app

# copy project
COPY . .

# add and run as non-root user
#RUN adduser -D myuser
#USER myuser

# run server
CMD sh start_azure_server.sh

here is my start script

#!/bin/sh

echo "Starting ssh daemon!!!"
/usr/sbin/sshd

echo "Collecting static and starting server!!!"

python manage.py collectstatic --noinput &&
daphne flexitkt.asgi:application -b 0.0.0.0 -p $PORT --proxy-headers
python manage.py rqworker

and here is my envirinment config

[
  {
    "name": "AWS_ACCESS_KEY",
    "value": "*****************",
    "slotSetting": false
  },
  {
    "name": "AWS_REGION",
    "value": "eu-north-1",
    "slotSetting": false
  },
  {
    "name": "AWS_SECRET_KEY",
    "value": "**********",
    "slotSetting": false
  },
  {
    "name": "CKEDITOR_CLOUDSERVICE_UPLOAD_TOKEN",
    "value": "*************",
    "slotSetting": false
  },
  {
    "name": "CKEDITOR_CLOUDSERVICE_UPLOAD_URL",
    "value": "*************",
    "slotSetting": false
  },
  {
    "name": "CKEDITOR_CLOUDSERVICE_WS_URL",
    "value": "**************",
    "slotSetting": false
  },
  {
    "name": "CKEDITOR_ENVIRONMENT_ID",
    "value": "*************",
    "slotSetting": false
  },
  {
    "name": "CKEDITOR_SECRET_KEY",
    "value": "***************",
    "slotSetting": false
  },
  {
    "name": "DATABASE_URL",
    "value": "postgres://*************:#############@**************.postgres.database.azure.com:5432/*******",
    "slotSetting": false
  },
  {
    "name": "DJANGOENV",
    "value": "staging",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_PASSWORD",
    "value": "***********",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_URL",
    "value": "https://index.docker.io",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_USERNAME",
    "value": "*******",
    "slotSetting": false
  },
  {
    "name": "EPI_SERVER_URL",
    "value": "*******************",
    "slotSetting": false
  },
  {
    "name": "FLEXIT_PAY_BAMBORA_ACCESS_TOKEN",
    "value": "*********",
    "slotSetting": false
  },
  {
    "name": "FLEXIT_PAY_BAMBORA_MD5_KEY",
    "value": "*******",
    "slotSetting": false
  },
  {
    "name": "FLEXIT_PAY_BAMBORA_MERCHANT_NUMBER",
    "value": "********",
    "slotSetting": false
  },
  {
    "name": "FLEXIT_PAY_BAMBORA_SECRET_TOKEN",
    "value": "*************",
    "slotSetting": false
  },
  {
    "name": "FLEXITKT_CAMPAIGNS_SUBDOMAIN",
    "value": "campaign-staging",
    "slotSetting": false
  },
  {
    "name": "FLEXITKT_CUSTOMERS_SUBDOMAIN",
    "value": "account-staging",
    "slotSetting": false
  },
  {
    "name": "FLEXITKT_MAIN_SUBDOMAIN",
    "value": "internt-staging",
    "slotSetting": false
  },
  {
    "name": "FLEXITKT_SITE_DOMAIN",
    "value": "*******************",
    "slotSetting": false
  },
  {
    "name": "FLEXITKT_USE_SUBDOMAINS",
    "value": "False",
    "slotSetting": false
  },
  {
    "name": "GOOGLE_API_URL_KEY",
    "value": "**********************",
    "slotSetting": false
  },
  {
    "name": "HERE_API_ACCESS_KEY_ID",
    "value": "******************",
    "slotSetting": false
  },
  {
    "name": "HERE_API_ACCESS_KEY_SECRET",
    "value": "*********************",
    "slotSetting": false
  },
  {
    "name": "HERE_MAPS_API_KEY",
    "value": "*********************",
    "slotSetting": false
  },
  {
    "name": "MICROSOFT_AUTH_CLIENT_ID",
    "value": "*******************",
    "slotSetting": false
  },
  {
    "name": "MICROSOFT_AUTH_CLIENT_SECRET",
    "value": "**********************",
    "slotSetting": false
  },
  {
    "name": "MICROSOFT_AUTH_REDIRECT_URL",
    "value": "*********************",
    "slotSetting": false
  },
  {
    "name": "MICROSOFT_FILE_PICKER_REDIRECT_URI",
    "value": "**********************",
    "slotSetting": false
  },
  {
    "name": "PORT",
    "value": "8000",
    "slotSetting": false
  },
  {
    "name": "PRODUCTS_SITE_URL",
    "value": "*************",
    "slotSetting": false
  },
  {
    "name": "REDIS_URL",
    "value": "***********************",
    "slotSetting": false
  },
  {
    "name": "S3_BUCKET_NAME",
    "value": "******************",
    "slotSetting": false
  },
  {
    "name": "SECRET_KEY",
    "value": "***********************",
    "slotSetting": false
  },
  {
    "name": "SEND_TO_ONLY_ALLOWED_ADDRESSES",
    "value": "True",
    "slotSetting": false
  },
  {
    "name": "SIEMENS_CLOUD_PASSWORD",
    "value": "**********************",
    "slotSetting": false
  },
  {
    "name": "SIEMENS_CLOUD_SUBSCRIPTION_KEY",
    "value": "***********************",
    "slotSetting": false
  },
  {
    "name": "SIEMENS_CLOUD_TENANT_ID",
    "value": "******************************",
    "slotSetting": false
  },
  {
    "name": "SIEMENS_CLOUD_USERNAME",
    "value": "******************************",
    "slotSetting": false
  },
  {
    "name": "WEBSITE_HTTPLOGGING_RETENTION_DAYS",
    "value": "10",
    "slotSetting": false
  },
  {
    "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
    "value": "false",
    "slotSetting": false
  },
  {
    "name": "WEBSITES_PORT",
    "value": "8000",
    "slotSetting": false
  }
]

Please check if you have edited the environment variables in the correct format in the app settings of the Azure app service.

In a default Linux container or a custom Linux container, any nested JSON key structure in the app setting name like ApplicationInsights:InstrumentationKey needs to be configured in App Service as ApplicationInsights__InstrumentationKey for the key name. In other words, any : should be replaced by __ (double underscore).

Reference: https://docs.microsoft.com/en-us/azure/app-service/configure-common#add-or-edit

Here it is for all those seeking the answer to the above. After a support session with Microsoft the problem has been solved.

I still don't get why an important issue, that appears to be well known to Microsoft, isn't covered in detail in either their docs or any of their tutorials, but here we are...

Since docker container is basically wrapped in another container that receives your env variables, you have to "manually" or, maybe better put "explicitly" copy over your environment from outer to inner container

And that is achieved by pasting next line in your startup.sh (or whatever you called your startup script)

eval $(printenv | sed -n "s/^\([^=]\+\)=\(.*\)$/export \1=\2/p" | sed 's/"/\\\"/g' | sed '/=/s//="/' | sed 's/$/"/' >> /etc/profile)

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