我正在尝试在docker中构建一个celery
容器,就像这样:
celery:
image: dev3_web
restart: always
volumes:
- ./services/web:/usr/src/app
- ./services/web/logs:/usr/src/app
command: celery worker -A celery_worker.celery --loglevel=INFO -Q cache
environment:
- CELERY_BROKER=redis://redis:6379/0
- CELERY_RESULT_BACKEND=redis://redis:6379/0
depends_on:
- web
- redis
links:
- redis:redis
redis:
image: redis:5.0.3-alpine
restart: always
expose:
- '6379'
ports:
- '6379:6379'
monitor:
image: dev3_web
ports:
- 5555:5555
command: flower -A celery_worker.celery --port=5555 --broker=redis://redis:6379/0
depends_on:
- web
- redis
从docker-compose-dev.yml
开始运行。
但是我遇到了错误:
celery_1 | Traceback (most recent call last):
celery_1 | File "/usr/bin/celery", line 10, in <module>
celery_1 | sys.exit(main())
celery_1 | File "/usr/lib/python3.6/site-packages/celery/__main__.py", line 16, in main
celery_1 | _main()
celery_1 | File "/usr/lib/python3.6/site-packages/celery/bin/celery.py", line 322, in main
celery_1 | cmd.execute_from_commandline(argv)
celery_1 | File "/usr/lib/python3.6/site-packages/celery/bin/celery.py", line 496, in execute_from_commandline
celery_1 | super(CeleryCommand, self).execute_from_commandline(argv)))
celery_1 | File "/usr/lib/python3.6/site-packages/celery/bin/base.py", line 273, in execute_from_commandline
celery_1 | argv = self.setup_app_from_commandline(argv)
celery_1 | File "/usr/lib/python3.6/site-packages/celery/bin/base.py", line 479, in setup_app_from_commandline
celery_1 | self.app = self.find_app(app)
celery_1 | File "/usr/lib/python3.6/site-packages/celery/bin/base.py", line 501, in find_app
celery_1 | return find_app(app, symbol_by_name=self.symbol_by_name)
celery_1 | File "/usr/lib/python3.6/site-packages/celery/app/utils.py", line 359, in find_app
celery_1 | sym = symbol_by_name(app, imp=imp)
celery_1 | File "/usr/lib/python3.6/site-packages/celery/bin/base.py", line 504, in symbol_by_name
celery_1 | return imports.symbol_by_name(name, imp=imp)
celery_1 | File "/usr/lib/python3.6/site-packages/kombu/utils/imports.py", line 56, in symbol_by_name
celery_1 | module = imp(module_name, package=package, **kwargs)
celery_1 | File "/usr/lib/python3.6/site-packages/celery/utils/imports.py", line 104, in import_from_cwd
celery_1 | return imp(module, package=package)
celery_1 | File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
celery_1 | return _bootstrap._gcd_import(name[level:], package, level)
celery_1 | File "<frozen importlib._bootstrap>", line 994, in _gcd_import
celery_1 | File "<frozen importlib._bootstrap>", line 971, in _find_and_load
celery_1 | File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
celery_1 | ModuleNotFoundError: No module named 'celery_worker'
文件夹结构:
web/
dockerfile
celery_worker.py
project/
__init__.py
web / celery_worker.py
#!/usr/bin/env python
import os
from project import celery, create_app
app = create_app()
app.app_context().push()
web / project / __ init __。py
import os
# third party libs
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from celery import Celery
from flask_debugtoolbar import DebugToolbarExtension
from flask_cors import CORS
from flask_migrate import Migrate
from flask_bcrypt import Bcrypt
from flask_mail import Mail
# instantiate the db
db = SQLAlchemy()
# background processes instance
celery = Celery(__name__, broker='redis://redis:6379/0')
# extensions
toolbar = DebugToolbarExtension()
cors = CORS()
migrate = Migrate()
bcrypt = Bcrypt()
mail = Mail()
def create_app(script_info=None):
from .api import routes
# instantiate the app
app = Flask(__name__)
# set config
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)
# set up extensions
db.init_app(app)
toolbar.init_app(app)
cors.init_app(app)
migrate.init_app(app, db)
bcrypt.init_app(app)
# register blueprints
routes.init_app(app)
#models.init_app(app)
celery.conf.update(app.config)
# shell context for flask cli
@app.shell_context_processor
def ctx():
return {'app': app, 'db': db}
return app
在我的 web / Dockerfile 中,我像这样设置工作目录:
(...)
# set working directory
WORKDIR /usr/src/app
(...)
在我使用docker容器之前,该设置曾经与flask
配合使用,并且在根目录下运行以下命令:
celery worker -A celery_worker.celery --loglevel=INFO -Q cache
我这里缺少什么吗?
答案 0 :(得分:0)
celery:
image: dev3_web
restart: always
volumes:
- ./services/web:/usr/src/app
- ./services/web/logs:/usr/src/app # <--- here is your problem
您正在加载compose文件中app目录上方的logs子目录中。列出的最后一个将获胜,因此您将只有一个空文件夹或一些日志文件,正在运行的容器中没有代码。