主机:Windows 10
我创建了一个使用sqlite数据库的django项目。我尝试使用以下dockerfile将django应用程序放入容器中:
FROM python:3.5
#Enviromental variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
#Work Dir
ADD . /code
WORKDIR /code
# Install dependencies
RUN pip install -r requirements.txt
Dockercompose文件:
version: '3.7'
services:
db:
image: postgres:11.1-alpine
volumes:
- pgdata:/var/lib/postgresql/data/
ports:
- 5432:5432
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
volumes:
pgdata:
现在,当我尝试运行代码时,它给了我这个错误:
Starting iomweb_db_1 ... done
Starting iomweb_web_1 ... done
Attaching to iomweb_db_1, iomweb_web_1
db_1 | 2018-12-31 20:26:15.535 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2018-12-31 20:26:15.535 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2018-12-31 20:26:15.547 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.
5432"
db_1 | 2018-12-31 20:26:15.576 UTC [19] LOG: database system was interrupted; last known up at 2018
-12-31 19:58:19 UTC
db_1 | 2018-12-31 20:26:16.655 UTC [19] LOG: database system was not properly shut down; automatic
recovery in progress
db_1 | 2018-12-31 20:26:16.662 UTC [19] LOG: redo starts at 0/17ABE00
db_1 | 2018-12-31 20:26:16.662 UTC [19] LOG: invalid record length at 0/17ABE38: wanted 24, got 0
db_1 | 2018-12-31 20:26:16.662 UTC [19] LOG: redo done at 0/17ABE00
db_1 | 2018-12-31 20:26:16.716 UTC [1] LOG: database system is ready to accept connections
web_1 | /usr/local/lib/python3.5/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wh
eel package will be renamed from release 2.8; in order to keep installing from binary please use "pip
install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-
install-from-pypi>.
web_1 | """)
web_1 | /usr/local/lib/python3.5/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wh
eel package will be renamed from release 2.8; in order to keep installing from binary please use "pip
install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-
install-from-pypi>.
web_1 | """)
web_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f4e1a0
75268>
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrap
per
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/runserver.py",
line 112, in inner_run
web_1 | autoreload.raise_last_exception()
web_1 | File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 248, in rais
e_last_exception
web_1 | raise _exception[1]
web_1 | File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 327,
in execute
web_1 | autoreload.check_errors(django.setup)()
web_1 | File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrap
per
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python3.5/site-packages/django/apps/registry.py", line 112, in populat
e
web_1 | app_config.import_models()
web_1 | File "/usr/local/lib/python3.5/site-packages/django/apps/config.py", line 198, in import_mo
dels
web_1 | self.models_module = import_module(models_module_name)
web_1 | File "/usr/local/lib/python3.5/importlib/__init__.py", line 126, in import_module
web_1 | return _bootstrap._gcd_import(name[level:], package, level)
web_1 | File "<frozen importlib._bootstrap>", line 985, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 968, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 697, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
web_1 | File "/usr/local/lib/python3.5/site-packages/registration/models.py", line 206, in <module>
web_1 | class RegistrationProfile(models.Model):
web_1 | File "/usr/local/lib/python3.5/site-packages/registration/models.py", line 222, in Registra
tionProfile
web_1 | user = models.OneToOneField(UserModelString(), verbose_name=_('user'))
现在,我可以在模型下的容器内部手动解决此问题;但我不确定docker容器从何处获取这些模型。我试图从系统中的python更改模型,但仍然这样做。我想修复此问题,因此不必在每次构建此容器时都手动修复此问题。我可以做一个泊坞窗提交;但仍然可以帮助您找出docker为什么要使用此models.py。
答案 0 :(得分:0)
我能够解决此问题。问题是我正在安装的django-registration-redux库与Django 2.0不兼容。一旦将此软件包更新为最新版本,一切都很好。
答案 1 :(得分:-1)
创建一个run.sh
文件并在下面写下;
python manage.py makemigrations && \
python manage.py migrate && \
python manage.py runserver 0.0.0.0:8000
然后在您的docker-compose.yml
文件中写下以下内容;
...
web:
build: .
command: sh run.sh
...