我目前正在用docker和Celeron生产我的django项目。为此,我在网上关注了一篇博客文章。
现在我无意中发现了一个我似乎无法纠正的错误。
当使用sqlite运行容器时,一切都很正常,但是使用postgresql,我得到了这个错误:
celery-beat_1 | [2020-03-20 14:32:00,052: INFO/MainProcess] Scheduler: Sending due task check_routers_online (Router.tasks.check_routers_online)
celery_1 | [2020-03-20 14:32:00,066: INFO/MainProcess] Received task: Router.tasks.check_routers_online[d4cece14-1d20-43ae-8712-1ad48ce79208]
celery_1 | [2020-03-20 14:32:00,085: ERROR/ForkPoolWorker-1] Task Router.tasks.check_routers_online[d4cece14-1d20-43ae-8712-1ad48ce79208] raised unexpected: OperationalError('no such table: Router_router')
celery_1 | Traceback (most recent call last):
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
celery_1 | return self.cursor.execute(sql, params)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
celery_1 | return Database.Cursor.execute(self, query, params)
celery_1 | sqlite3.OperationalError: no such table: Router_router
celery_1 |
celery_1 | The above exception was the direct cause of the following exception:
celery_1 |
celery_1 | Traceback (most recent call last):
celery_1 | File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 385, in trace_task
celery_1 | R = retval = fun(*args, **kwargs)
celery_1 | File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 650, in __protected_call__
celery_1 | return self.run(*args, **kwargs)
celery_1 | File "/home/app/web/Router/tasks.py", line 14, in check_routers_online
celery_1 | for router in routers:
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 276, in __iter__
celery_1 | self._fetch_all()
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
celery_1 | self._result_cache = list(self._iterable_class(self))
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 57, in __iter__
celery_1 | results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
celery_1 | cursor.execute(sql, params)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
celery_1 | return super().execute(sql, params)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
celery_1 | return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
celery_1 | return executor(sql, params, many, context)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
celery_1 | return self.cursor.execute(sql, params)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
celery_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
celery_1 | return self.cursor.execute(sql, params)
celery_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
celery_1 | return Database.Cursor.execute(self, query, params)
celery_1 | django.db.utils.OperationalError: no such table: Router_router
这是我的docker撰写文件:
version: '3.3'
services:
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=***
- POSTGRES_PASSWORD=*****
- POSTGRES_DB=***
redis:
image: "redis:alpine"
web:
container_name: inventurliste_app
build: .
command: gunicorn Inventur.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/home/app/web/staticfiles
expose:
- 8000
env_file:
- .env
nginx:
container_name: inventurliste_nginx
build: ./nginx
volumes:
- static_volume:/home/app/web/staticfiles
ports:
- 1337:80
depends_on:
- web
- db
celery:
build: .
command: celery -A Inventur worker -l info
depends_on:
- redis
- db
celery-beat:
build: .
command: celery -A Inventur beat -l info
depends_on:
- redis
- db
volumes:
static_volume:
postgres_data:
celery.py
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Inventur.settings')
app = Celery('Inventur')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
我的套装中的芹菜部分.py
CELERY_BROKER_URL = 'redis://redis:6379'
CELERY_RESULT_BACKEND = 'redis://redis:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_BEAT_SCHEDULE = {
'check_routers_online': {
'task': 'Router.tasks.check_routers_online',
'schedule': crontab() # execute every minute
},
'refresh_all_ports': {
'task': 'Switch.tasks.refresh_all_ports',
'schedule': crontab(minute="0", hour="*/1") # execute every hour
}
}
设置中的数据库部分.py
DATABASES = {
"default": {
"ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"),
"NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
"USER": os.environ.get("SQL_USER", "user"),
"PASSWORD": os.environ.get("SQL_PASSWORD", "password"),
"HOST": os.environ.get("SQL_HOST", "localhost"),
"PORT": os.environ.get("SQL_PORT", "5432"),
}, }
.env
DEBUG=0
SECRET_KEY=******
DJANGO_ALLOWED_HOSTS=***
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=**
SQL_USER=***
SQL_PASSWORD=***
SQL_HOST=db
SQL_PORT=5432
makemigrations告诉我没有变化迁移也无济于事。
我不能让这个在sqlite中运行,所以我希望有人能帮助我。
EDIT:添加了数据库部分的设置和.env文件
哦,Django本身也很好。postgres不存在数据库问题只有芹菜似乎有问题
看起来我已经修复了。
我也不得不将.env文件添加到celeni服务中。
更新的docker组成:
version: '3.3'
services:
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=***
- POSTGRES_PASSWORD=*****
- POSTGRES_DB=***
redis:
image: "redis:alpine"
web:
container_name: inventurliste_app
build: .
command: gunicorn Inventur.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/home/app/web/staticfiles
expose:
- 8000
env_file:
- .env
nginx:
container_name: inventurliste_nginx
build: ./nginx
volumes:
- static_volume:/home/app/web/staticfiles
ports:
- 1337:80
depends_on:
- web
- db
celery:
build: .
command: celery -A Inventur worker -l info
depends_on:
- redis
- db
env_file: # here i added the .env
- .env
celery-beat:
build: .
command: celery -A Inventur beat -l info
depends_on:
- redis
- db
env_file: # and here i had to add it too
- .env
volumes:
static_volume:
postgres_data: