docker django、redis和celery

2ledvvac  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(565)

我不知道我遗漏了什么部分,但是当我运行docker compose时celery 没有连接到redis——构建错误:无法连接到redis://127.0.0.1:6379/0:错误111连接到127.0.0.1:6379。连接被拒绝。
这是我的文件docker-compose.yml

version: '3'

  services:
    web:
      build: .
      image: resolution
      depends_on:
        - db
        - redis
        - celery
      command: bash -c "python3 /code/manage.py migrate && python3 /code/manage.py initialsetup && python3 /code/manage.py runserver 0.0.0.0:8000"
      volumes:
        - .:/code
      ports:
        - "8000:8000"
      links:
        - db:db
        - redis:redis
        - celery:celery
      restart: always
      environment:
        - POSTGRES_DB=postgres
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
        - PGHOST=trust
        - PGPORT=5432
    db:
      image: postgres:latest
      environment:
        POSTGRES_DB: 'postgres'
        POSTGRES_PASSWORD: 'postgres'
        POSTGRES_USER: 'postgres'
        POSTGRES_HOST: 'trust'
    redis:
      image: "redis:alpine"
      ports:
      - "6379:6379"
      restart: on-failure
    celery:
      image: resolution
      command: celery -A mayan worker -l info
      environment:
        - DJANGO_SETTINGS_MODULE=mayan.settings.production
      volumes:
        - .:/code
      depends_on:
        - db
        - redis
      links:
        - redis:redis
      restart: on-failure
xqkwcwgp

xqkwcwgp1#

celery 以及 redis 在不同的容器中运行。
根据您共享的错误消息,很可能是 celery 正在尝试连接到 localhost 到达 RedisDB ,它不在本地主机上。
搜索包含 CELERY_BROKER_URL 以及 CELERY_RESULT_BACKEND 价值观。很可能是这样的:

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'

它们应该是这样的,指向您在compose文件中定义的redis服务名称:

CELERY_BROKER_URL = 'redis://redis:6379'
CELERY_RESULT_BACKEND = 'redis://redis:6379'

如果您没有这样的配置,请直接搜索celery 示例初始化的位置,并确保它如下所示:

app = Celery('server', broker='redis://redis:6379/0')

相关问题