我找到了一个Django项目,但无法通过以下方式在Docker容器中运行它:
git clone https://github.com/hotdogee/django-blast.git
$ cat requirements.txt
#psycopg2==2.6.0 # 如果使用 postgreSQL 作为数据库后端
我有以下 Dockerfile:
FROM python:2.7
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
接下来我运行:
$ docker run --name some-django-app -p 8000:8000 -d my-django-app
4202d7b2b47603e32dc800d3fbedbfaf4363f8cafe45684242aa3bb2b0eba257
最后,我输入http://localhost:8000/
并得到了Firefox can’t establish a connection to the server at localhost:8000
。
我错过了什么?
先感谢您
答案1
在您的 requirements.txt 中,将 kombo 版本更改为 3.0.30。这将解决第一个错误:
我刚刚重现了你的步骤。通过执行docker 日志一些 django 应用程序我见过这样的情况:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/src/app/i5k/__init__.py", line 5, in <module>
from .celery import app as celery_app
File "/usr/src/app/i5k/celery.py", line 5, in <module>
from celery import Celery
File "/usr/local/lib/python2.7/site-packages/celery/__init__.py", line 130, in <module>
from celery import five
File "/usr/local/lib/python2.7/site-packages/celery/five.py", line 149, in <module>
from kombu.utils.compat import OrderedDict # noqa
File "/usr/local/lib/python2.7/site-packages/kombu/utils/__init__.py", line 19, in <module>
from uuid import UUID, uuid4 as _uuid4, _uuid_generate_random
ImportError: cannot import name _uuid_generate_random
这似乎正是这个问题。
当你修复 kombo 版本时,会出现一个新的错误(使用 docker logs 检查):
[root@:/.../django-blast]# docker logs test
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python2.7/site-packages/django/apps/config.py", line 112, in create
mod = import_module(mod_path)
File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/local/lib/python2.7/site-packages/django/contrib/postgres/apps.py", line 7, in <module>
from .signals import register_hstore_handler
File "/usr/local/lib/python2.7/site-packages/django/contrib/postgres/signals.py", line 1, in <module>
from psycopg2 import ProgrammingError
ImportError: No module named psycopg2
而且看起来您不想使用的包是需要的......
# grep -ir psycopg2
webapollo/models.py:import psycopg2
webapollo/models.py: conn = psycopg2.connect('dbname=' + self.db_name + ' user=' + self.db_acct + ' host=' + self.host)
webapollo/models.py: conn = psycopg2.connect('dbname=' + species.db_name + ' user=' + species.db_acct + ' host=' + species.host)
webapollo/models.py: conn = psycopg2.connect('dbname=' + species.db_name + ' user=' + species.db_acct + ' host=' + species.host)
i5k/settings_prod.py: 'ENGINE': 'django.db.backends.postgresql_psycopg2',
i5k/settings.py: 'ENGINE': 'django.db.backends.postgresql_psycopg2',
您可以继续以这种方式工作,或者完全迁移到较新的 Python 版本。