Ted Yu
Ted Yu

Reputation: 1804

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. It works fine with command python main.py.

So I plan to deploy it under docker, Dockerfile as below:

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

COPY ./src/* /app/

Build and run the web app in docker, I got error:

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? Did I miss something?

Upvotes: 1

Views: 941

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146490

The problem is your COPY statement. 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. So you will not get the files correctly. So you should be using COPY ./src /app

Upvotes: 2

Related Questions