At startup Redis does not work, therefore caching does not work, as I realized there is no connection to the database. I can't make loaddata fixtures and Celery can't connect to Redis either. In general, nothing works. No matter how many examples I found on how to create these files, everywhere it is done like this.
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
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'
You should create a network in your Docker Compose file and add all of your services to that network. Then, modify the addresses in your Docker Compose according to the service. For example, change
CELERY_BROKER_URL: 'redis://127.0.0.1:6379'
to
CELERY_BROKER_URL: 'redis://redis:6379'