在Docker容器中找不到Python模块

时间:2019-11-20 14:59:07

标签: python django docker docker-compose cookiecutter-django

我正在尝试运行一个django-cookiecutter项目,但是在某些容器中却出现了ModuleNotFoundError: No module named 'iprestrict'错误。我检查了我们的存储库,使我的同事可以使用Windows的Docker和Linux的Docker成功构建和运行。我已经调试了一段时间,但是现在我茫然了,要求您的帮助。

这是我的设置:

$ uname -r
5.0.0-36-generic

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 19.04
Release:    19.04
Codename:   disco

$ docker -v
Docker version 19.03.3, build a872fc2f86

$ docker-compose -v
docker-compose version 1.25.0, build 0a186604

$ service docker status
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-11-20 13:02:07 CET; 1h 59min ago
     Docs: https://docs.docker.com
 Main PID: 9382 (dockerd)
    Tasks: 41
   Memory: 518.6M
   CGroup: /system.slice/docker.service
           ├─ 9382 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
           ├─14817 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 5432 -container-ip 172.30.0.2 -container-port 5432
           └─14829 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8025 -container-ip 172.30.0.3 -container-port 8025

为获得一个全新的开始,我在项目目录中运行了以下命令:

$ docker-compose down --volumes
$ docker-compose build --no-cache

该项目已成功构建,没有错误消息,这是最后几行,已删除了一堆软件包以提高可读性。可以看到,django-iprestrict已成功安装:

Successfully installed [...] django-environ-0.4.5 django-extensions-2.1.5 django-iprestrict-1.7.0 django-model-utils-3.1.2 [...]

这是Dockerfile中的相关部分:

FROM python:3.6.8-alpine

ENV PYTHONUNBUFFERED 1

[...]

COPY ./requirements /requirements
RUN pip install -r /requirements/local.txt

local.txt中的软件包中有:

[...]
django==2.1.5  # https://www.djangoproject.com/
django-environ==0.4.5  # https://github.com/joke2k/django-environ
django-model-utils==3.1.2  # https://github.com/jazzband/django-model-utils
django-iprestrict==1.7.0  # https://github.com/muccg/django-iprestrict
[...]

来自docker-compose.yml

services:
  django:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    image: companyname_local_django
    depends_on:
      - postgres

    # these configs need to be overriden by the local and prod service configurations
    volumes:
      - .:/app
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres

并且来自docker-compose.override.yml

services:
  django: &django
    ports:
      - "8000:8000"
    command: /start
    environment:
      - TERM=xterm-256color
    restart: on-failure

    # these configs are overriding the values in docker-compose.yml django service configuration
    volumes:
      - webpack_build:/app/build
      - .:/app
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres

  postgres:
    volumes:
      - local_postgres_data:/var/lib/postgresql/data
      - local_postgres_data_backups:/backups
    ports:
      - "5432:5432"

  celeryworker:
    <<: *django
    image: companyname_local_celeryworker
    depends_on:
      - postgres
    ports: []
    command: /start-celeryworker

  celerybeat:
    <<: *django
    image: companyname_local_celerybeat
    depends_on:
      - postgres
    ports: []
    command: /start-celerybeat

  flower:
    <<: *django
    image: companyname_local_flower
    ports:
      - "5555:5555"
    command: /start-flower

然后我尝试使用$ docker-compose up运行它,并得到以下错误消息:

celerybeat_1    | celery beat v4.2.1 (windowlicker) is starting.
celerybeat_1    | Traceback (most recent call last):
celerybeat_1    |   File "/usr/local/bin/celery", line 10, in <module>
celerybeat_1    |     sys.exit(main())
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/__main__.py", line 16, in main
celerybeat_1    |     _main()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 322, in main
celerybeat_1    |     cmd.execute_from_commandline(argv)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 496, in execute_from_commandline
celerybeat_1    |     super(CeleryCommand, self).execute_from_commandline(argv)))
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 275, in execute_from_commandline
celerybeat_1    |     return self.handle_argv(self.prog_name, argv[1:])
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 488, in handle_argv
celerybeat_1    |     return self.execute(command, argv)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 420, in execute
celerybeat_1    |     ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 279, in run_from_argv
celerybeat_1    |     sys.argv if argv is None else argv, command)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 363, in handle_argv
celerybeat_1    |     return self(*args, **options)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 238, in __call__
celerybeat_1    |     ret = self.run(*args, **kwargs)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/bin/beat.py", line 109, in run
celerybeat_1    |     return beat().run()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/apps/beat.py", line 79, in run
celerybeat_1    |     self.init_loader()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/apps/beat.py", line 128, in init_loader
celerybeat_1    |     self.app.loader.init_worker()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/loaders/base.py", line 114, in init_worker
celerybeat_1    |     self.import_default_modules()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/loaders/base.py", line 108, in import_default_modules
celerybeat_1    |     raise response
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/utils/dispatch/signal.py", line 288, in send
celerybeat_1    |     response = receiver(signal=self, sender=sender, **named)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/fixups/django.py", line 82, in on_import_modules
celerybeat_1    |     self.worker_fixup.validate_models()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/fixups/django.py", line 120, in validate_models
celerybeat_1    |     self.django_setup()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/celery/fixups/django.py", line 116, in django_setup
celerybeat_1    |     django.setup()
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
celerybeat_1    |     apps.populate(settings.INSTALLED_APPS)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
celerybeat_1    |     app_config = AppConfig.create(entry)
celerybeat_1    |   File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
celerybeat_1    |     module = import_module(entry)
celerybeat_1    |   File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
celerybeat_1    |     return _bootstrap._gcd_import(name[level:], package, level)
celerybeat_1    |   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
celerybeat_1    |   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
celerybeat_1    |   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
celerybeat_1    | ModuleNotFoundError: No module named 'iprestrict'

celeryworker_1flower_1容器的错误。

然后,我检查了安装在不同容器中的软件包,发现iprestrictcelerybeatceleryworker中缺少flower,但django中却没有(它们会展开):

$ docker-compose run --rm django sh
/app $ pip freeze | grep django-
django-allauth==0.38.0
django-cors-headers==2.4.0
django-coverage-plugin==1.6.0
django-crispy-forms==1.7.2
django-debug-toolbar==1.11
django-environ==0.4.5
django-extensions==2.1.5
django-iprestrict==1.7.0
django-model-utils==3.1.2
django-react-templatetags==5.2.1
django-redis==4.10.0
django-simple-history==2.7.0
django-webpack-loader==0.6.0

/app $ python
Python 3.6.8 (default, Feb  6 2019, 01:56:13) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import iprestrict
>>> 
$ docker-compose run --rm flower sh
/app $ pip freeze | grep django-
django-allauth==0.38.0
django-cors-headers==2.4.0
django-coverage-plugin==1.6.0
django-crispy-forms==1.7.2
django-debug-toolbar==1.11
django-environ==0.4.5
django-extensions==2.1.5
django-model-utils==3.1.2
django-react-templatetags==5.2.1
django-redis==4.10.0
django-simple-history==2.7.0
django-webpack-loader==0.6.0

/app $ python
Python 3.6.8 (default, Feb  6 2019, 01:56:13) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import iprestrict
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'iprestrict'
>>> 

如您所见,所有具有local.txt中定义的正确版本的软件包都在那里除外 django-iprestrict==1.7.0

有人知道这可能是什么原因吗?谢谢!

0 个答案:

没有答案