创建 Docker 映像时找不到 Flask 应用程序 [英] Could not locate a Flask application when creating a Docker Image

查看:31
本文介绍了创建 Docker 映像时找不到 Flask 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人能帮我解决这个问题.当我尝试运行 docker run todolist-docker 时出现以下错误代码.

I hope someone can help me with this. The following error code comes up when I try and run docker run todolist-docker.

   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off

Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

我的文件夹目录在这里:

My folder directory is here:

Folder name: todoflaskappfinal
__pycache__
static
templates
venv
App.py
Dockerfile
requirements.txt
todo.db
ToDoList.db

在 todoflaskappfinal 文件夹中,我有一个 Dockerfile 文件:

within the todoflaskappfinal folder, I have a Dockerfile file:

# syntax=docker/dockerfile:1

FROM python:3.7.8

WORKDIR /tododocker

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

在 App.py 中,我已经正确设置了所有内容(我假设),显然这中间有更多代码.

And within App.py, I've set up everything (I assume) correctly, obviously with more code between this.

#Website Configuration
app = Flask(__name__)

if __name__ == "__main__":
  app.run(debug=True, port=5000, host='0.0.0.0')

我已经将 FLASK_APP 设置为 App.py,使用 venv 等制作了虚拟环境.当我在终端中输入 flask run 时,它会正确加载网站并在 127.0 上显示它.0.01.但是,当我尝试使用 docker build --tag todolist-docker 命令然后 docker run todolist-docker 时,错误消息出现在上面.谁能看出我做错了什么?

I've set FLASK_APP as App.py, made the virtual environment with venv, etc. When I type in flask run in the terminal it loads the website up correctly and displays it on 127.0.0.01. However, when I try and use the docker build --tag todolist-docker command and then docker run todolist-docker, the error message appears above. Can anyone see what I'm doing wrong?

推荐答案

docker 容器中是否定义了 FLASK_APP?Dockerfile 中没有 ENV 语句,您也没有提到使用 docker 的 -e 或 --env 命令选项.您的容器不会从托管环境继承您的环境变量.

Is FLASK_APP defined in the docker container? There's no ENV statement in the Dockerfile, and you didn't mention using docker's -e or --env command option. Your container won't inherit your environment variables from the hosting environment.

# syntax=docker/dockerfile:1

FROM python:3.7.8

WORKDIR /tododocker

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .
# Add this:
ENV FLASK_APP=App.py

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

这篇关于创建 Docker 映像时找不到 Flask 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆