我把我的Flask 应用程序停靠在了Windows上.我使用Docker Desktop来运行Docker.在Windows中,要加入容器,只能使用IP"192.168.99.100",而不能使用本地主机.因此,在对接APP后,我try 从该IP地址访问一个API,但请求超时:

enter image description here

The strange thing is that I enabled debug inside my main.py file, but when I run docker-compose up, it seems that it doesnt' run the application in debug mode: enter image description here

我的Dockerfile,在其中我指定了在哪里可以找到我的main.py应用程序(与flaskapp相同的路径):

FROM python:3.8

WORKDIR /app


COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

EXPOSE 8080

ENV FLASK_APP=main.py
CMD ["flask", "run", "--host", "0.0.0.0"]

Docker-compose.yaml

version: "3"

services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    expose: 
      - "8080"
    volumes:
      - .:/app
    stdin_open: true
    environment:
        - FLASK_APP=main.py
        - GOOGLE_APPLICATION_CREDENTIALS=cred.json

Docker-compose.yaml and Dockerfile are in the same path.

Py,在那里您可以看到我在调试模式下运行它的最后几行:

import os, io
import logging
import json
import jinja2
import requests
import re

from flask import Flask, request, render_template, send_file, abort, session, redirect, jsonify, url_for
from flask_cors import CORS
from flask.helpers import make_response
from google.cloud import tasks_v2, bigquery, firestore, kms_v1, storage

import httplib2shim
import env_config
from audience_manager import AudienceManager
from datetime import datetime, timedelta
import time
import mysql.connector
import cron
from google.cloud import pubsub_v1
import google.oauth2.credentials
from googleapiclient.discovery import build
from google.auth import app_engine
import google_auth_oauthlib.flow

from exceptions import AuthenticatorException, UserVisibilityNotFoundException, InvalidEmailException, \
    NoneEmailException
from jwt.exceptions import ExpiredSignatureError
from authlib.integrations.base_client import MismatchingStateError
from authlib.integrations.flask_client import OAuth
from oauth2_authentication import Oauth2Authentication

from services import db_service
from spreadsheet import Spreadsheet
from ggm import start_ggm_check, ggm_module
import secret_manager_module

from flask.globals import g

from task_module import CloudTask

import iap_service

httplib2shim.patch()

app = Flask(__name__)

@app.before_request
def before_request():
    print("#### abc")
        
    g.auth_user = "giacomo.brunetta@external.stellantis.com"


@app.route('/hello', methods=['POST'])
def ciao():
    print("## hello")
    return 'ok'

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

要求中的Flask 版本为2.0.1

推荐答案

问题是,这一点:

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

永远不会执行(有关详细信息,请参见What does if name == "main": do?).


要让事情运转起来,你有两个 Select .

  1. 使用Flask 命令行工具:

    ...
    EXPOSE 8080
    ENV FLASK_APP=main.py
    ENV FLASK_DEBUG=1
    ENV FLASK_RUN_PORT=8080
    CMD ["flask","run","--host=0.0.0.0"]
    
  2. 使用Python :

    ...
    EXPOSE 8080
    CMD ["python3", "main.py"]
    

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

Python相关问答推荐

在有限数量的唯一字母的长字符串中,找到包含重复不超过k次的所有唯一字母的最长子字符串

Python:根据创建时间合并两个收件箱

如何从. text中进行pip安装跳过无法访问的库

Altair -箱形图边界设置为黑色,中线设置为红色

如何处理必须存在于环境中但无法安装的Python项目依赖项?

机器人与Pyton Minecraft服务器状态不和

如何终止带有队列的Python进程?+ 队列大小的错误?

查找下一个值=实际值加上使用极点的50%

如何根据另一列值用字典中的值替换列值

如何在Deliveryter笔记本中从同步上下文正确地安排和等待Delivercio代码中的结果?

如何使用symy打印方程?

连接两个具有不同标题的收件箱

在Python中处理大量CSV文件中的数据

Pandas 有条件轮班操作

使用setuptools pyproject.toml和自定义目录树构建PyPi包

用砂箱开发Web统计分析

CommandeError:模块numba没有属性generated_jit''''

从Windows Python脚本在WSL上运行Linux应用程序

ConversationalRetrivalChain引发键错误

导入错误:无法导入名称';操作';