简体   繁体   English

http://localhost/5000 在 docker flask 中不起作用

[英]http://localhost/5000 not working in docker flask

can't open file '/web/manage.py': [Errno 2] No such file or directory exited with code 2无法打开文件'/web/manage.py':[Errno 2]没有这样的文件或目录退出代码2

NOTE: Tried all similar problems solution posted, did not work.注意:尝试了发布的所有类似问题的解决方案,但没有奏效。

No matter what I do, not able to get http://localhost/5000 to work.无论我做什么,都无法让 http://localhost/5000 工作。 Even if the above error goes away by removing volume and command from docker-container.即使上述错误通过从 docker-container 中删除卷和命令而消失。

Below is docker-compose.yml下面是docker-compose.yml


services:
  web:
    build: ./web
    command: python /web/manage.py runserver 0.0.0.0:8000
    volumes:
      - './users:/usr/src/app'
 
    ports:
      - 5000:5000 
    env_file:
      - ./.env.dev 

Below is Dockerfile:下面是 Dockerfile:

# pull official base image
FROM python:3.9.5-slim-buster

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# copy project
COPY . /usr/src/app/

BELOW IS manage.py:下面是manage.py:

from flask.cli import FlaskGroup
from project import app
cli = FlaskGroup(app)
if __name__ == "__main__":
    cli()

BELOW IS init .py:下面是初始化.py:

from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def hello_world():
    return jsonify(hello="world")

Below is the structure:下面是结构:

The ones marked in red appeared when I ran this command: docker-compose build enter image description here当我运行此命令时,出现红色标记的那些: docker-compose build enter image description here

A couple of changes to do.要做一些改变。

The cli command in your docker-compose.yml file needs to be: docker-compose.yml文件中的 cli 命令需要是:

    command: python /usr/src/app/manage.py run -h 0.0.0.0 -p 8000

There the command name is run and not runserver .那里的命令名称是run而不是runserver Also the host ip to bind and port to listen are configured as different command options.此外,要绑定的host ip 和要侦听的port被配置为不同的命令选项。

Also the configured port mapping for the service needs to map to the container port from the command:此外,为服务配置的端口映射需要从命令 map 到容器端口:

    ports:
      - 5000:8000 

In your manage.py module, FlaskGroup should be provided create_app option which is factory not the app instance.在您的manage.py模块中,应为FlaskGroup提供create_app选项,该选项是工厂而不是应用程序实例。 You can implement this as a lambda function.您可以将其实现为 lambda function。

cli = FlaskGroup(create_app=(lambda:app))

Edit编辑

The source files are not mounted in the container volume that why you're getting "no such file manage.py ".源文件没有安装在容器卷中,这就是为什么你得到“没有这样的文件manage.py ”。

You need to mount your source files in the container volume under /usr/src/app .您需要将源文件安装在/usr/src/app下的容器卷中。

volumes:
  - './web:/usr/src/app' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM