I am raising a server using docker-compose, Django, Redis and other services, but I am not able to connect with Redis. Redis container goes up without errors, but I can't connect my django backend to it.
First I tried to configure my .env file like this:
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
This resulted in this error when I try to call a Redis task from the backend:
Error 111 connecting to 127.0.0.1:6379. Connection refused
Searching I found this answer: docker-compose + django + redis - Error 111 connecting to 127.0.0.1:6379. Connection refused
Basically they teach how to configure the composer like this:
environment:
- REDIS_HOST=redis
I tried and the result was this:
Error -3 connecting to redis:6379. Temporary failure in name resolution.
I noticed that my database is managing to get the PostgreSQL address without needing the environment, in the composer configuration. My postgreSQL in composer looks like this:
services:
db:
env_file:
- .env
image: postgres
environment:
- POSTGRES_DB=${DATABASE_NAME}
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
ports:
- "${DATABASE_PORT}:${DATABASE_PORT}"
expose:
- "${DATABASE_PORT}"
command: -p ${DATABASE_PORT}
backend:
(...)
Notice that I named it as db
With that I can access it in my .env file like this:
DATABASE_HOST=db
DATABASE_PORT=5433
This works with the PostgreSQL, so I tried to do the same with Redis:
services:
(...)
redis_store:
env_file:
- .env
image: redis
ports:
- "${REDIS_PORT}:${REDIS_PORT}"
And in .env I did it like this:
REDIS_HOST=redis_store
REDIS_PORT=6379
But again the result was this:
Error 111 connecting to redis_store:6379. Connection refused
I understood the idea behind declaring a environment:
environment:
- REDIS_HOST=redis
But I didn't understand why it didn't work, nor why PostgreSQL would work without it and calling db directly.
If anyone can help me to resolve these doubts I would appreciate it.
Here is my current complete configuration:
version: '3'
services:
db:
env_file:
- .env
image: postgres
environment:
- POSTGRES_DB=${DATABASE_NAME}
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
ports:
- "${DATABASE_PORT}:${DATABASE_PORT}"
expose:
- "${DATABASE_PORT}"
command: -p ${DATABASE_PORT}
backend:
build: .
command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn --bind 0.0.0.0:${GUNICORN_PORT} some_project.wsgi:application"
container_name: some_project_be
env_file:
- .env
volumes:
- .:/some_project_be
ports:
- "${GUNICORN_PORT}:${GUNICORN_PORT}"
expose:
- "${GUNICORN_PORT}"
image: some_project
depends_on:
- db
- redis_store
nginx:
env_file:
- .env
environment:
- NGINX_PORT=${NGINX_PORT}
- GUNICORN_PORT=${GUNICORN_PORT}
command: /bin/sh -c "envsubst '$${NGINX_PORT} $${GUNICORN_PORT}' < /etc/nginx/conf.d/nginx.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
build: ./nginx
ports:
- "${NGINX_PORT}:${NGINX_PORT}"
depends_on:
- backend
restart: "on-failure"
redis_store:
env_file:
- .env
image: redis
ports:
- "${REDIS_PORT}:${REDIS_PORT}"
celery:
env_file:
- .env
restart: always
build:
context: .
command: celery -A some_project worker --pool=solo -l info
links:
- redis_store
volumes:
- .:/some_project_be
depends_on:
- db
- redis_store
- backend
And here my .env:
REDIS_HOST=redis_store
REDIS_PORT=6379
DATABASE_HOST=db
DATABASE_PORT=5432
DATABASE_NAME=some_db_name
DATABASE_USER=some_name
DATABASE_PASSWORD=some_secret
DATABASE_USER_EMAIL=xxx@gmail.com
NGINX_PORT=81
GUNICORN_PORT=8000
GOOGLE_MAPS_API_KEY=some_key
PROJECT_NAME=some_project