gpt4 book ai didi

python - Flask Docker应用程序不提供API并且在Docker镜像内未找到文件

转载 作者:行者123 更新时间:2023-12-02 19:18:41 24 4
gpt4 key购买 nike

我有一个flask应用程序,想通过Docker对其进行容器化。通常,我使用flask run运行该应用程序,并且该应用程序在端口8888上提供API。但是,当我尝试使用docker运行该应用程序时,该API无法启动。我使用docker-compose build --no-cache构建图像,并开始使用docker-compose -f docker-compose.yml up,容器启动。我使用docker exec -it covid-api /bin/bash,我到达root@d0659f0c1627:/app#,而ls不返回任何内容。它是空的。

这是我的文件夹结构:

enter image description here

以下是我的docker配置:

FROM python:3.7-stretch
WORKDIR /app

COPY ./badproxy /etc/apt/apt.conf.d/99fixbadproxy

RUN apt-get update \
&& apt-get install -y curl apt-utils gcc build-essential apt-transport-https ca-certificates \
&& curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update \
&& ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev mssql-tools locales \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
&& locale-gen

COPY .flaskenv ./
COPY requirements.txt ./
RUN pip install -r requirements.txt

ENV FLASK_ENV=development
ENV PATH="/opt/mssql-tools/bin:${PATH}"

COPY . ./

CMD python setup.py develop

CMD ["flask", "run", "-h", "0.0.0.0", "-p", "8888"]

Docker组成:
version: '3.7'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- '6000:6000'
environment:
SECRET_BASE_KEY: 'development-secret'
volumes:
- .:/app
restart: unless-stopped
networks:
mynetwork:
aliases:
- web.myname.test

networks:
mynetwork:
driver: bridge

src / app.py:
import logging.config

import os
from flask import Flask, Blueprint
from flask_cors import CORS
from werkzeug.middleware.proxy_fix import ProxyFix
from src.config import default
from src.api.controllers.endpoints.users import ns as users_namespace
from src.api.controllers.endpoints.statuses import ns as status_namespace
from src.api import api
from src.database import db

app = Flask(__name__)
CORS(app)
app.wsgi_app = ProxyFix(app.wsgi_app)
logging_conf_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '../logging.conf'))
logging.config.fileConfig(logging_conf_path)
log = logging.getLogger(__name__)


def configure_app(flask_app):
flask_app.config['SERVER_NAME'] = default.FLASK_SERVER_NAME
flask_app.config['SQLALCHEMY_DATABASE_URI'] = default.SQLALCHEMY_DATABASE_URI
flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = default.SQLALCHEMY_TRACK_MODIFICATIONS
flask_app.config['SWAGGER_UI_DOC_EXPANSION'] = default.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
flask_app.config['RESTPLUS_VALIDATE'] = default.RESTPLUS_VALIDATE
flask_app.config['RESTPLUS_MASK_SWAGGER'] = default.RESTPLUS_MASK_SWAGGER
flask_app.config['ERROR_404_HELP'] = default.RESTPLUS_ERROR_404_HELP

return flask_app


def initialize_app(flask_app):
configure_app(flask_app)

blueprint = Blueprint('CovidAPI', __name__, url_prefix='/')
api.init_app(blueprint)
api.add_namespace(users_namespace)
api.add_namespace(status_namespace)
flask_app.register_blueprint(blueprint)

db.init_app(flask_app)

return flask_app


app = initialize_app(app)

# def main():
# initialize_app(app)
# log.info('>>>>> Starting development server at http://{}/ <<<<<'.format(app.config['SERVER_NAME']))
# app.run(debug=default.FLASK_DEBUG)


if __name__ == "__main__":
log.info('>>>>> Starting development server at http://{}/ <<<<<'.format(app.config['SERVER_NAME']))
app.run(debug=default.FLASK_DEBUG)

请指教。非常感谢您的帮助。

最佳答案

可能的问题是您的项目没有MANIFEST.in文件。当setup.py执行时,它会寻找其他文件以包含到您的项目中。完整的说明可以在here中找到。

对于您的项目文件夹,它可能看起来像这样。

recursive-include src/*
recursive-include src/api *
recursive-include src/api/controllers *
recursive-include src/api/controllers/endpoints *

关于python - Flask Docker应用程序不提供API并且在Docker镜像内未找到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61598024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com