1

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?

1
  • how do you import function defined in module_a? Commented Sep 1, 2017 at 11:57

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.