简体   繁体   English

Docker Compose 找不到 pip 安装 package

[英]Docker Compose cannot find pip installed package

Docker Compose gives ModuleNotFoundError: No module named 'django' error. Docker Compose 给出ModuleNotFoundError: No module named 'django'错误。 Docker Compose has not installed my pip installed packages pip install -r requirements.txt but running the image any other way shows they are installed and this issue is only with docker-compose, why? Docker Compose has not installed my pip installed packages pip install -r requirements.txt but running the image any other way shows they are installed and this issue is only with docker-compose, why?

Compose撰写

version: '3.8'

services:
  web:
    build: ./
    user: python
    volumes:
      - ./:/app
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev

Dockerfile Dockerfile

# Base image  
FROM python:3.9.6

ENV HOME=/app

# create directory for the app user
RUN mkdir -p $HOME

# set work directory
WORKDIR $HOME
 
# install psycopg2 dependencies
RUN apt-get update \
    && apt-get -y install libpq-dev gcc \
    && pip install psycopg2 \
    && apt-get -y install gunicorn3

RUN pip install --upgrade pip
ADD requirements*.txt .
RUN pip install -r requirements.txt
COPY python . .

ENTRYPOINT ["/app/entrypoint.sh"]

EXPOSE 8000

Problem:问题:

I have created the following Dockerfile which runs in production and even runs locally outside of docker-compose without any issues ie the following works with no errors docker run -p 8000:8000 web/lastest .我创建了以下 Dockerfile 它在生产中运行,甚至在 docker-compose 之外本地运行,没有任何问题,即以下工作没有任何错误docker run -p 8000:8000 web/lastest

However, when I run this via docker-compose it fails to find my installed pip packages.但是,当我通过 docker-compose 运行它时,它找不到我安装的 pip 包。

For example:例如:

  • docker-compose build (successful) docker-compose 构建(成功)
  • docker-compose up docker-compose向上

Error错误

web_1  | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
web_1  | [2022-01-04 14:55:05 +0000] [1] [INFO] Starting gunicorn 20.1.0
web_1  | [2022-01-04 14:55:05 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web_1  | [2022-01-04 14:55:05 +0000] [1] [INFO] Using worker: sync
web_1  | [2022-01-04 14:55:05 +0000] [8] [INFO] Booting worker with pid: 8
web_1  | [2022-01-04 14:55:05 +0000] [8] [ERROR] Exception in worker process
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
web_1  |     worker.init_process()
web_1  |   File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 134, in init_process
web_1  |     self.load_wsgi()
web_1  |   File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
web_1  |     self.wsgi = self.app.wsgi()
web_1  |   File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 67, in wsgi
web_1  |     self.callable = self.load()
web_1  |   File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
web_1  |     return self.load_wsgiapp()
web_1  |   File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
web_1  |     return util.import_app(self.app_uri)
web_1  |   File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 384, in import_app
web_1  |     mod = importlib.import_module(module)
web_1  |   File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
web_1  |     return _bootstrap._gcd_import(name[level:], package, level)
web_1  |   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
web_1  |   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
web_1  |   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
web_1  |   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
web_1  |   File "<frozen importlib._bootstrap_external>", line 790, in exec_module
web_1  |   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
web_1  |   File "/app/app/wsgi.py", line 12, in <module>
web_1  |     from django.core.wsgi import get_wsgi_application
web_1  | ModuleNotFoundError: No module named 'django' 

Running which python outputs /usr/local/bin/python on both running the image directly and using docker-compose.在直接运行映像和使用 docker-compose 时运行which python输出/usr/local/bin/python

Running docker run -it 43d991d65c02 /bin/bash I can see and run Django.运行docker run -it 43d991d65c02 /bin/bash我可以看到并运行 Django。 Only when running docker-compose is Django not installed, why?只有在运行 docker-compose 时才没有安装 Django,为什么?

In the containerfile presented, we work in the container-directory /app .在提供的容器文件中,我们在容器目录/app中工作。 But at runtime, we mount a volume to /app .但是在运行时,我们将一个卷挂载到/app Hence, all content that is generated during image build time that is stored in /app is overridden by the volume mount.因此,存储在/app中的映像构建期间生成的所有内容都会被卷安装覆盖。 If the dependencies at runtime were installed in /app , then they are overridden by the volume mount.如果运行时的依赖项安装在/app中,那么它们会被卷安装覆盖。

To fix this issue, two possibilities come to my mind:为了解决这个问题,我想到了两种可能性:

  1. We can remove the volume mount.我们可以删除卷挂载。 This will, however, devoid us of the capability of "hot reloading".然而,这将使我们失去“热重载”的能力。

  2. We can re-run pip install -r requirements.txt at container startup, before starting the application.我们可以在容器启动时重新运行pip install -r requirements.txt ,然后再启动应用程序。 This would mean adding the line pip install -r requirements.txt to the entrypoint.sh -script.这意味着将行pip install -r requirements.txt添加到entrypoint.sh -script。

When docker-compose mounts the volume to /app folder its previous structure become hidden and new structure overrides previous one.当 docker-compose 将卷挂载到 /app 文件夹时,其先前的结构将被隐藏,新结构将覆盖先前的结构。

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

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