Error Connecting to Redis Service

Hello,
Someone can help me?
I have this error after the deployment of my API on Koyeb.

Exception Value: Error -2 connecting to cfa-redis-prod:6379. Name or service not known.

My Docker Compose file contains two services: API and Redis. Our API requires a connection to the Redis service in order to function properly.

In the first service, i’m using the variable : REDIS_DB: redis://cfa-redis-prod:6379/0

Here is my docker-compose.yml content :

version: “3.8”

services:
cfa-api-prod:
container_name: cfa-api-prod
image: cfa-api-prod
build:
context: .
dockerfile: ./docker/Dockerfile
restart: on-failure
ports:
- ${PROD_PORT}:8000
env_file:
- .env
environment:
LAUNCH_TYPE: webserver
DB_HOST: ${DB_HOST}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_DATABASE}
DB_PORT: ${DB_PORT}
REDIS_DB: redis://cfa-redis-prod:6379/0
CELERY_BROKER_URL: redis://cfa-redis-prod:6379/1
DB_ENGINE: django.db.backends.postgresql
depends_on:
- cfa-redis-prod
command: >
bash -c "
python3 manage.py makemigrations django_cron &&
python3 manage.py migrate &&
python3 manage.py collectstatic --noinput &&
gunicorn SpectorlyAPI.wsgi:application -w 3 -b 0.0.0.0:8000 --reload"
networks:
- cfa_network_prod

cfa-redis-prod:
image: redis:latest
container_name: cfa-redis-prod
ports:
- 6379:6379
env_file:
- .env
volumes:
- redis_data:/data
networks:
- cfa_network_prod

volumes:
postgres-data:
redis_data:
networks:
cfa_network_prod:
driver: bridge

hello!

How do you deploy your service? Can you make a screenshot of the koyeb service settings page?

Hi @Malick_A ,

The error says that the name of the redis server is not known. This is not unexpected, because docker compose exposes the various containers as names that are resolvable by the other containers. The koyeb platform does expose the services in a mesh, so that each can communicate with the other, but using a different logic.
You have to do two things, in order for your API to communicate with redis:

  • you are deploying redis as a koyeb service: go to the redis service page and find the name that ends with .koyeb. The logic with which it is composed is ..koyeb
  • you have to ensure that the port that you use to communicate to redis is exposed to the mesh. To do so: you have to add a port in the redis service settings page, without exposing it to the outside world (so you have to deactivate the “public” toggle in the port configuration). The default port for redis is the one in the url: 6379.

A world of caution: koyeb services are not (yet) stateful. If your API relies on redis to keep its state, this will not work, because as soon as you redeploy redis it will lose its state (there is no persistent storage yet).

I hope this helps.