Gunicorn containerCommand not working for AzureWebAppContainer, cannot import flask module

Viewed 16

I have a dockerized flask app that I run with Gunicorn. Locally everything works fine by running the Gunicorn command in the docker-compose.yml. However, when I enter the same command in the AzureWebAppContainer@1 task's containerCommand field, it does not recognize the python flaskr module anymore.

The folder structure is as follows:


project/
├─ docker-compose.yml/
├─ azure-pipeline.yml/
├─ form/
│  ├─ flaskr
│  │  ├─ __init__.py
│  │  ├─ module1.py
│  │  ├─ module2.py
│  │  ├─ module2.py
│  ├─ templates
│  ├─ Dockerfile
│  ├─ requirements.txt

Where in the init.py I have the method:


def create_app(test_config=None):
    \# create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    return app

In my docker-compose.yml is as follows:


version: "3.7"
services:
  project:
    build:
      context: ./project
      dockerfile: Dockerfile
    container_name: project
    volumes:
      \- ./project:/usr/src/app
    env_file:
      \- project/\_env.env
    ports:
      \- 5000:8080
    command: gunicorn -b 0.0.0.0:8080 flaskr:create_app()
    restart: always

Which works fine locally.

However, when I try to deploy the built container to an Azure Web App with the following pipeline:


- task: AzureWebAppContainer@1
  displayName: 'Azure Web App on Container Deploy'
  inputs:
    azureSubscription: $(azureSubscription)
    appName: $(appName)
    imageName: $(pullRegistry)/$(imageRepository):$(Build.SourceVersion)
    containerCommand: gunicorn -b 0.0.0.0:8080 flaskr:create_app()

I am getting the following error:


2022-09-09T15:25:42.727Z INFO  - Pull Image successful, Time taken: 0 Minutes and 56 Seconds
2022-09-09T15:25:42.799Z INFO  - Starting container for site
2022-09-09T15:25:42.800Z INFO  - docker run -d --expose=80 --name wa-project-test_0_ac4d8bdf -e DOCKER_CUSTOM_IMAGE_NAME=project.azurecr.io/project:758 -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=wa-project-test -e WEBSITE_AUTH_ENABLED=False -e PORT=80 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=wa-project-test.azurewebsites.net -e WEBSITE_INSTANCE_ID=xxx-e WEBSITE_USE_DIAGNOSTIC_SERVER=False project.azurecr.io/project:xxxxx  gunicorn -b 0.0.0.0:8080 flaskr:create_app()
2022-09-09T15:25:42.801Z INFO  - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2022-09-09T15:25:48.801102626Z \[2022-09-09 15:25:48 +0000\] \[1\] \[INFO\] Starting gunicorn 20.1.0
2022-09-09T15:25:48.805756581Z \[2022-09-09 15:25:48 +0000\] \[1\] \[INFO\] Listening at: http://0.0.0.0:8080 (1)
2022-09-09T15:25:48.805795943Z \[2022-09-09 15:25:48 +0000\] \[1\] \[INFO\] Using worker: sync
2022-09-09T15:25:48.824303764Z \[2022-09-09 15:25:48 +0000\] \[7\] \[INFO\] Booting worker with pid: 7
2022-09-09T15:25:48.830298138Z \[2022-09-09 15:25:48 +0000\] \[7\] \[ERROR\] Exception in worker process
2022-09-09T15:25:48.830335202Z Traceback (most recent call last):
2022-09-09T15:25:48.830344993Z   File "/usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2022-09-09T15:25:48.830352886Z     worker.init_process()
2022-09-09T15:25:48.830359979Z   File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process
2022-09-09T15:25:48.830367172Z     self.load_wsgi()
2022-09-09T15:25:48.830373266Z   File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2022-09-09T15:25:48.830380060Z     self.wsgi = self.app.wsgi()
2022-09-09T15:25:48.830386653Z   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2022-09-09T15:25:48.830394346Z     self.callable = self.load()
2022-09-09T15:25:48.830401539Z   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2022-09-09T15:25:48.830408333Z     return self.load_wsgiapp()
2022-09-09T15:25:48.830414527Z   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2022-09-09T15:25:48.830421420Z     return util.import_app(self.app_uri)
2022-09-09T15:25:48.830428413Z   File "/usr/local/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app
2022-09-09T15:25:48.830475668Z     mod = importlib.import_module(module)
2022-09-09T15:25:48.830485159Z   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
2022-09-09T15:25:48.830492053Z     return \_bootstrap.\_gcd_import(name\[level:\], package, level)
2022-09-09T15:25:48.830498247Z   File "\<frozen importlib.\_bootstrap\>", line 1014, in \_gcd_import
2022-09-09T15:25:48.830505440Z   File "\<frozen importlib.\_bootstrap\>", line 991, in \_find_and_load
2022-09-09T15:25:48.830512433Z   File "\<frozen importlib.\_bootstrap\>", line 973, in \_find_and_load_unlocked
2022-09-09T15:25:48.830520426Z ModuleNotFoundError: No module named 'flaskr'
2022-09-09T15:25:48.830526620Z \[2022-09-09 15:25:48 +0000\] \[7\] \[INFO\] Worker exiting (pid: 7)
2022-09-09T15:25:48.883451664Z \[2022-09-09 15:25:48 +0000\] \[1\] \[INFO\] Shutting down: Master
2022-09-09T15:25:48.884026615Z \[2022-09-09 15:25:48 +0000\] \[1\] \[INFO\] Reason: Worker failed to boot.
2022-09-09T15:25:48.709Z INFO  - Initiating warmup request to container wa-project-test_0_ac4d8bdf for site wa-project-test
2022-09-09T15:25:49.756Z ERROR - Container wa-project-test_0_ac4d8bdf for site wa-project-test has exited, failing site start
2022-09-09T15:25:49.836Z ERROR - Container wa-project-test_0_ac4d8bdf didn't respond to HTTP pings on port: 80, failing site start. See container logs for debugging.
2022-09-09T15:25:49.889Z INFO  - Stopping site wa-project-test because it failed during startup.

I get this error more often with Gunicorn when I try to run directly in the Dockerfile instead of through docker-compose. Has anyone encountered this before or knows a solution?

0 Answers
Related