cannot run python flask app in docker with vscode (from tutorial)

  docker, flask, python-3.x, visual-studio-code

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"]

Source: Docker Questions

LEAVE A COMMENT