简体   繁体   English

找不到在docker + nginx + uwsgi下部署的Flask应用程序的模块

[英]Cannot find module of flask app deployed under docker+nginx+uwsgi

I have a project written in flask, with structure like: 我有一个用烧瓶写的项目,其结构如下:

-/
 |- src
   |- __init__.py
   |- main.py
   |- module_a
       |- __init__.py
       |- ...
       |- ...
   |- web
       |- __init__.py
       |- web.py
 |- Dockerfile

The file main.py calls entry function defined in web/web.py , and web.py calls business function defined in module_a . 文件main.py调用在web/web.py定义的入口函数,而web.py调用在module_a定义的业务函数。 It works fine with command python main.py . 使用python main.py命令可以正常工作。

So I plan to deploy it under docker, Dockerfile as below: 因此,我计划将其部署在Dockerfile下,如下所示:

FROM tiangolo/uwsgi-nginx-flask:python3.6

COPY ./src/* /app/

Build and run the web app in docker, I got error: 在docker中构建并运行Web应用程序,出现错误:

Traceback (most recent call last):
  File "./main.py", line 1, in <module>
    from web import run
  File "./web.py", line 5, in <module>
    import module_a
ModuleNotFoundError: No module named 'module_a'

Why did uwsgi cannot find module_a ? 为什么module_a无法找到module_a Did I miss something? 我错过了什么?

The problem is your COPY statement. 问题是您的COPY语句。 I created a sample with your data 我用您的数据创建了一个样本

FROM alpine
COPY ./src/* /app/
RUN ls -alh /app
COPY ./src /app
RUN ls -alh /app

If you build you will see the output 如果您进行构建,则将看到输出

Step 1/5 : FROM alpine
 ---> 7328f6f8b418
Step 2/5 : COPY ./src/* /app/
 ---> Using cache
 ---> ad9fbfdc161d
Step 3/5 : RUN ls -alh /app
 ---> Using cache
 ---> 4dcad7cf4fba
Step 4/5 : COPY ./src /app
 ---> d25b4dc34f82
Removing intermediate container 4bf0fc884332
Step 5/5 : RUN ls -alh /app
 ---> Running in 34401d92bf03
total 16
drwxr-xr-x    4 root     root        4.0K Sep  1 16:46 .
drwxr-xr-x   26 root     root        4.0K Sep  1 16:46 ..
-rw-rw-r--    1 root     root           0 Sep  1 16:44 __init__.py
-rw-rw-r--    1 root     root           0 Sep  1 16:44 main.py
drwxrwxr-x    2 root     root        4.0K Sep  1 16:45 module_a
drwxrwxr-x    2 root     root        4.0K Sep  1 16:45 web
-rw-rw-r--    1 root     root           0 Sep  1 16:45 web.py

When you use ./src/* it will copy contents of those matches to the /app. 当您使用./src/* ,它将那些匹配项的内容复制到/ app。 So you will not get the files correctly. 因此,您将无法正确获取文件。 So you should be using COPY ./src /app 所以你应该使用COPY ./src /app

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

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