0

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
Andre Maia
  • 152
  • 1
  • 16

1 Answers1

0

It looks like I had some other container interfering. I deleted all the images and build them again. The configuration that works is this:

redis_store:
   env_file:
      - .env
    image: redis
    ports:
      - "${REDIS_PORT}:${REDIS_PORT}"
    command: --port ${REDIS_PORT}
    expose:
      - "${REDIS_PORT}"

Notice that I now have an "expose" and a "command". They allow me to change the redis door and run Redis, say, on port 6378

I don't need to define the variable in the "environment", I can simply use the service name in my .env file as follows:

REDIS_HOST=redis_store
REDIS_PORT=6379

The lesson here is as follows. I am new to the docker and did not correctly remove old images and containers and probably one of them was interfering.

I stopped having problems when I dropped all the containers and deleted all the old images and volumes with the following commands:

Stop all containers:

sudo docker stop $(sudo docker ps -a -q)

Remove images and volumes:

sudo docker rm $(sudo docker ps -a -q) && \
sudo docker image rm $(sudo docker image ls -a -q) && \
sudo docker volume rm $(sudo docker volume ls -q)
Andre Maia
  • 152
  • 1
  • 16
  • The topic that helped me to set up the redis correctly in doker-compose were this: https://stackoverflow.com/questions/57116745/docker-compose-django-redis-error-111-connecting-to-127-0-0-16379-connec – Andre Maia Mar 31 '21 at 05:46
  • This other helped me to allow the exchange of the Redis door: https://stackoverflow.com/questions/56135881/changing-redis-port-in-docker-compose-not-working – Andre Maia Mar 31 '21 at 05:48