1

I build a sample flask app by this tutorial
Afterwards I tried to build a docker out of it and followed the steps in this tutorial

The app runs in debug mode in localhost.
The docker is build without any error.
When I try to run the docker in debug mode, there pops up an error and I do not know why

 * Serving Flask app "hello_app\__init__.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.

Error: Could not import "hello_app\__init__".

I tried "hello_app\__init__:app" as well as "hello_app\__webapp__:app" as entrypoint in the dockerfile - always the same issue.

here my project structure:

enter image description here

__ init__.py:

from flask import Flask
app = Flask(__name__) # Flask instance named app

webapp.py:

# Entry point for the application.
from . import app    # For application discovery by the 'flask' command.
from . import views  # For import side-effects of setting up routes.

launch.json:

{"version": "0.2.0", "configurations": [{"name": "Python: Flask", "type": "python", "request": "launch", "module": "flask", "env": {"FLASK_APP": "hello_app/webapp", "FLASK_ENV": "development"}, "args": ["run", "--no-debugger"], "jinja": true}, {"name": "Docker: Python - Flask", "type": "docker", "request": "launch", "preLaunchTask": "docker-run: debug", "python": {"pathMappings": [{"localRoot": "${workspaceFolder}", "remoteRoot": "/app"}], "projectType": "flask"}}]}

Dockerfile:

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim-buster

EXPOSE 5000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "hello_app\__init__:app"]
2
  • Shouldn't the last argument to gunicorn be the Python module.name:function that produces the WSGI application? Does just hello_app:app work, without the implied __init__ and without the backslash? Commented Jul 16, 2021 at 17:27
  • no, not working :/ Commented Jul 16, 2021 at 18:12

1 Answer 1

0

added entry to launch.json (under "Docker: Python - Flask") and it works:

        "env": {
            "FLASK_APP": "hello_app/webapp.py",
            "FLASK_ENV": "development"
        },

and in dockerfile: "hello_app\__webapp__:app"

Sign up to request clarification or add additional context in comments.

3 Comments

Didn't work for me. And you are not explaining why.
and you dont say anything about your failure. Should I have been guessed what your issue will be in the future when I wrote this? hmm ... In most cases it is a problem of the adopter
In the docker terminal, there is the entry --env "FLASK_APP=hello_app\__init__.py" Perhaps this is the issue. This must be cached somewhere in the VSCode environment. It's not in the launch.json file, but it was in the original Dockerfile before I started fiddling with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.