Docker
Django 導致無法與 Docker 連接
我找到了一個 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
.我錯過了什麼?
先感謝您
在您的 requirements.txt 中,將 kombo 版本更改為 3.0.30。這將解決第一個錯誤:
我剛剛複制了你的步驟。通過做docker logs some-django-app我已經看到了:
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 日誌檢查):
[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 版本。