redis 无法在Docker中启动Django项目

0wi1tuuw  于 12个月前  发布在  Redis
关注(0)|答案(1)|浏览(156)

Redis在启动时不工作,因此缓存不工作,因为我意识到没有连接到数据库。我不能加载数据固定装置,Celery也不能连接到Redis。一般来说,什么都不起作用。无论我找到多少关于如何创建这些文件的例子,到处都是这样做的。

  1. Dockerfile:
FROM python:3.11

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

CMD python manage.py migrate \
    && python manage.py loaddata site_blog/fixtures/posts.json \
    && python manage.py loaddata site_blog/fixtures/post_category.json \
    && python manage.py loaddata site_blog/fixtures/comments.json \
    && python manage.py loaddata users/fixtures/users.json \
    && python manage.py makemigrations \
    && python manage.py migrate \
    && python manage.py runserver 0.0.0.0:8000

字符串

  1. docker-compose.yml:
version: '3'
services:
  postgres-db:
    image: postgres
    environment:
      POSTGRES_USER: db_mp_blog_username
      POSTGRES_PASSWORD: db_mp_blog_password
      POSTGRES_DB: db_mp_blog
    ports:
      - "5432:5432"
    restart: always

  redis:
    image: redis
    restart: always
    ports:
      - "6379:6379"

  site:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    depends_on:
      - postgres-db
      - redis
    environment:
      DB_NAME: "db_mp_blog"
      DB_USER: "db_mp_blog_username"
      DB_PASSWORD: "db_mp_blog_password"
      DB_HOST: "postgres-db"
      DB_PORT: 5432
      CELERY_BROKER_URL: 'redis://127.0.0.1:6379'
      CELERY_RESULT_BACKEND: 'redis://127.0.0.1:6379'
      SECRET_KEY: 'django-insecure-c467_p2n3g4ni%yq_ebaqe1j2=!ozmn=zvgh8i23uzos&9p3pc'
    restart: always

  celery:
    build: .
    command: celery -A blog worker --loglevel=info
    depends_on:
      - site
      - redis
      - postgres-db
    environment:
      DB_NAME: "db_mp_blog"
      DB_USER: "db_mp_blog_username"
      DB_PASSWORD: "db_mp_blog_password"
      DB_HOST: "postgres-db"
      DB_PORT: 5432
      CELERY_BROKER_URL: 'redis://127.0.0.1:6379'
      CELERY_RESULT_BACKEND: 'redis://127.0.0.1:6379'
      SECRET_KEY: 'django-insecure-c467_p2n3g4ni%yq_ebaqe1j2=!ozmn=zvgh8i23uzos&9p3pc'

inkz8wg9

inkz8wg91#

您应该在Docker Compose文件中创建一个网络,并将所有服务添加到该网络。然后,根据服务修改Docker Compose中的地址。例如,更改

CELERY_BROKER_URL: 'redis://127.0.0.1:6379'

字符串

CELERY_BROKER_URL: 'redis://redis:6379'

相关问题