[solved] Python django app doesn't deploy, fails to find python module

Hi all,
I’m migrating a django app to fly.io from Heroku. I have a ModuleNotFoundError that I cannot explain.

fly deploy
==> Verifying app config
--> Verified app config
==> Building image
Remote builder fly-builder-crimson-breeze-9492 ready
==> Creating build context
--> Creating build context done
==> Building image with Docker
--> docker host: 20.10.12 linux x86_64
[+] Building 0.9s (0/1)                                                         
[+] Building 2.5s (10/10) FINISHED                                                                                              
 => [internal] load remote build context                                                                                   0.0s
 => copy /context /                                                                                                        0.5s
 => [internal] load metadata for docker.io/library/python:3.10-slim-buster                                                 0.6s
 => [1/7] FROM docker.io/library/python:3.10-slim-buster@sha256:95f2dd6b380d0762cba090f5167259543dc156f08b8a85262926a2919  0.0s
 => CACHED [2/7] RUN mkdir -p /code                                                                                        0.0s
 => CACHED [3/7] WORKDIR /code                                                                                             0.0s
 => CACHED [4/7] COPY requirements.txt /tmp/requirements.txt                                                               0.0s
 => CACHED [5/7] RUN set -ex &&     pip install --upgrade pip &&     pip install -r /tmp/requirements.txt &&     rm -rf /  0.0s
 => [6/7] COPY . /code/                                                                                                    0.5s
 => ERROR [7/7] RUN python manage.py collectstatic --noinput                                                               0.7s
------
 > [7/7] RUN python manage.py collectstatic --noinput:
#10 0.674 Traceback (most recent call last):
#10 0.674   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 259, in fetch_command
#10 0.674     app_name = commands[subcommand]
#10 0.675 KeyError: 'collectstatic'
#10 0.675 
#10 0.675 During handling of the above exception, another exception occurred:
#10 0.675 
#10 0.675 Traceback (most recent call last):
#10 0.675   File "/code/manage.py", line 22, in <module>
#10 0.675     main()
#10 0.675   File "/code/manage.py", line 18, in main
#10 0.675     execute_from_command_line(sys.argv)
#10 0.675   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
#10 0.675     utility.execute()
#10 0.675   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
#10 0.675     self.fetch_command(subcommand).run_from_argv(self.argv)
#10 0.675   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 266, in fetch_command
#10 0.675     settings.INSTALLED_APPS
#10 0.675   File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 92, in __getattr__
#10 0.675     self._setup(name)
#10 0.675   File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 79, in _setup
#10 0.675     self._wrapped = Settings(settings_module)
#10 0.675   File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
#10 0.675     mod = importlib.import_module(self.SETTINGS_MODULE)
#10 0.675   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
#10 0.675     return _bootstrap._gcd_import(name[level:], package, level)
#10 0.676   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
#10 0.676   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
#10 0.676   File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
#10 0.676 ModuleNotFoundError: No module named 'dopmanager.settings'
------
Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c python manage.py collectstatic --noinput]: exit code: 1

I have deployed successfully the django demo app. I’ve tried to limit any change in the variables by reusing the same Dockerfile than the demo app, with the same requirements.txt file just in case the issue came from a dependency or something…
I cannot figure this one out.

Might be a silly issue but I’ve been struggling for the bigger part of the day now so I’m turning to you.

Here is the bugged Dockerfile:

ARG PYTHON_VERSION=3.10-slim-buster

FROM python:${PYTHON_VERSION}

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir -p /code

WORKDIR /code

COPY requirements.txt /tmp/requirements.txt

RUN set -ex && \
    pip install --upgrade pip && \
    pip install -r /tmp/requirements.txt && \
    rm -rf /root/.cache/

COPY . /code/

RUN python manage.py collectstatic --noinput

EXPOSE 8000

# replace demo.wsgi with <project_name>.wsgi
CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "dopmanager.wsgi"]

The working demo Dockerfile I’m using is litterally identical. I’ve even managed to change the name of the demo project to make sure I was able to change the gunicorn settings, and it still works.

My latest idea is that my files actually aren’t copied to the docker but I don’t know how to check that.

Any pointer appreciated, and if you need more info I’ll be glad to provide it!

Thanks a lot
Paul

Well lo and behold this issue was on me.
For some unknown reason the file dopmanager/settings.py had been added to .dockerignore so it definitely wasn’t where python was looking for it.
All is well now, sorry for the disturbance.