我正在学习docker,我想组成我的python django docker镜像以在Mac中运行 我已经创建了3个文件:
1:docker-compose
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
2:Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
3:要求
Django>=1.8,<2.0
psycopg2
然后我运行sudo docker-compose run web django-admin.py startproject composeexample .
,并在目录中创建了官方示例文件
但是当我运行docker-compose
我收到一个错误,它运行失败
web_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fcafcf47510>
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
web_1 | autoreload.raise_last_exception()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
web_1 | six.reraise(*_exception)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/six.py", line 685, in reraise
web_1 | raise value.with_traceback(tb)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 27, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 85, in populate
web_1 | app_config = AppConfig.create(entry)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 94, in create
web_1 | module = import_module(entry)
web_1 | File "/usr/local/lib/python3.7/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 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 4, in <module>
web_1 | from django.contrib.admin.filters import (
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/filters.py", line 10, in <module>
web_1 | from django.contrib.admin.options import IncorrectLookupParameters
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 13, in <module>
web_1 | from django.contrib.admin import helpers, widgets
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/widgets.py", line 152
web_1 | '%s=%s' % (k, v) for k, v in params.items(),
web_1 | ^
web_1 | SyntaxError: Generator expression must be parenthesized
现在有人可以帮助我吗?我很困惑。 谢谢。
答案 0 :(得分:1)
尽管我将我的requirements.txt文件更改为:
Django> = 2
psycopg2
还有其他建议吗?我几乎没有想法了。我一直在使用https://docs.docker.com/compose/django/#connect-the-database。据我所知,我已经根据该网页完成了本书的所有工作。
答案 1 :(得分:1)
此特定错误是fixed in django 2.1
您将需要升级到django>=2.1
,以确保获得此新版本。
关于提交的注释表明,此补丁将不回传到不支持python3.7的django 1.11.x。
根据the FAQ,Django 1.11.x与Python 3.7不兼容。
Django 1.11.x于2017年12月2日到达end of mainstream support,并且仅收到数据丢失和安全修补程序,直到使用寿命终止。
答案 2 :(得分:0)
由于django版本与python3.7之间的不一致,我已经自行解决了此问题。只需从requiere.txt文件中删除django的版本并安装最新版本即可。