5

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
  }
]
Jerry Shaw
  • 256
  • 4
  • 9

2 Answers2

10

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)
Jerry Shaw
  • 256
  • 4
  • 9
  • I'm still not entirely sure why this worked for me, but it did. After days of troubleshooting my azure container app service and not seeing the env variables in the container, this worked. Thanks for posting! – stoneMaster Aug 26 '21 at 13:40
  • This should have been here. https://learn.microsoft.com/en-us/azure/app-service/configure-custom-container?tabs=debian&pivots=container-linux. Pretty dissapointed with msdocs – klido Feb 09 '23 at 14:14
0

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://learn.microsoft.com/en-us/azure/app-service/configure-common#add-or-edit

Nancy
  • 26,865
  • 3
  • 18
  • 34
  • All my variable names are all caps with underscores, e.g. DATABASE_URL – Jerry Shaw Dec 03 '20 at 08:30
  • Could you show these steps that I can reproduce it? – Nancy Dec 03 '20 at 08:42
  • not quite sure how you want me to show you those steps. It's a django app in an Alpine based container. The application runs fine, and it sees all the variables since it is able to connect to database and all. But when I ssh to the container to run some management commands, I get errors because env variables are not there – Jerry Shaw Dec 03 '20 at 08:44
  • Could you show the screenshots of the error message? What's your env variables? What's your Dockerfile configuration? – Nancy Dec 03 '20 at 09:43
  • I've edited description and provided relevant files and config. And of course I've edited out the actual secrets ;-) – Jerry Shaw Dec 04 '20 at 08:07