'ModuleNotFoundError: No module named '<project name>'.

Infinity-blue·2023년 11월 6일
0

Problem

도커 이미지를 실행 후 docker logs 를 출력하였더니 제목과 같은 에러가 뜬다.
'docker-compose.yml', Dockerfile은 다음과 같다.

Dockerfile

...
 services:
   django:
    volumes:
     - .:/app
     - tryon_static_volume:/app/static
     - tryon_media_volume:/app/media
   nginx:
    volumes:
     - tryon_static_volume:/app/static
     - tryon_media_volume:/app/media
     - /etc/docker_certs:/etc/nginx/ssl
     ...

docker-compose.yml

...
# Set the working directory inside the Django container.
WORKDIR /app

# Install system dependencies
RUN apt-get update
..
# Copy the requirements file to the container
COPY requirements.txt /app/

# Install Python dependencies
RUN pip install -r requirements.txt

# Copy the Django application files to the container
COPY . /app/

# Set the working directory to where manage.py is located
WORKDIR /app/Backend/tryon

# Set a default port in case one isn't provided
ENV PORT=8000
# Collect static files
RUN python manage.py collectstatic --noinput

# Document that the service listens on port 8000.
EXPOSE 8000  

# Start the application
CMD gunicorn tryon.wsgi:application --bind 0.0.0.0:$PORT

Troubleshooting steps

settings.py의 'INSTALLED_APPS' 부분에 장고 프로젝트 이름이 동일하게 설정되어 있는지 확인하였다. 또한, 'requirements.txt' 대신 'pipfile'을 사용하여 파이썬 의존성을 설치하였다. 문제를 해결하는 도중 발생한 별개의 오류에 대해선 아래의 링크에 정리를 해 놓았다.
Dlib error

Solution

Python이 장고 프로젝트를 인식 못하는 문제이다.
Dockerfile 을 아래와 같이 수정하였다. 장고 프로젝트 파일을 컨테이너에 복사하고 장고 프로젝트 파일의 위치를 알려주기 위하여 PYTHONPATH 변수를 아래와 같이 추가하였다. 또한 'Pipenv run..'을 사용하여 이 변수가 적용된 환경에서 프로젝트를 실행하도록 하였다.

docker-compose.yml

...
FROM python:3.7-slim

# Install Pipenv
RUN pip install pipenv

# Copy Pipfile and Pipfile.lock
COPY Backend/Pipfile /app/Backend/
COPY Backend/Pipfile.lock /app/Backend/

# Install Python dependencies from Pipfile into the container's environment.
# '--deploy' ensures that 'Pipfile.lock' is up to date.
RUN cd /app/Backend && pipenv install --deploy

# Copy the Django application files to the container
COPY Backend/tryon /app/Backend/tryon

# Set environment variables so that Python can find Django project.
# When 'pipenv run..' starts app as below, the variables should be already set and applied. 
ENV PORT=8000 \
	PYTHONPATH=/app/Backend/tryon \
	DJANGO_SETTINGS_MODULE=tryon.settings

# Document that the service listens on port 8000.
EXPOSE 8000  

# Set the working directory to where manage.py is located
# WORKDIR /app/Backend/tryon

# Collect static files
RUN cd /app/Backend/tryon && pipenv run python manage.py collectstatic --noinput

# Start the application. Ensure that the application is accessible from outside the container.
# CMD is executed once container is started from the resulting image.
# 'Pipenv run' runs the command in the context of the environment set up by 'pipenv.'
CMD cd /app/Backend/tryon && pipenv run gunicorn tryon.wsgi:application --bind 0.0.0.0:$PORT

Dockerfile

...
 volumes:
      - .:/app
      # The path on the right is set based on where Django files are copied to the container, for which Dockerfile is reponsible.
      - tryon_static_volume:/app/Backend/tryon/static
      - tryon_media_volume:/app/Backend/tryon/media



Follow-Up Actions

워낙 장고 프로젝트 디렉토리가 복잡하다 보니 구조가 헷갈려서 경로를 잘못 입력하곤 했었다.
다음부터는 프로젝트 디렉토리 구조를 미리 문서화 해야겠다.

0개의 댓글