Home > OS >  Django docker container could not connect to redis server on port 6379
Django docker container could not connect to redis server on port 6379

Time:08-05

I have a redis container running on port 6379 but my django application can't connect to that port.

CONTAINER ID   IMAGE                            COMMAND                  CREATED          STATUS          PORTS                                       NAMES
f62c020fec99   sp_ask_dashboard_celery_worker   "sh -c 'cd sp_dashbo…"   39 minutes ago   Up 39 minutes                                               sp_ask_admin_dashboard-master_celery_worker_1
60089763fd3f   ask_webapp                       "sh -c 'python sp_da…"   39 minutes ago   Up 39 minutes   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp   ask_dashboard_web
62cde1811827   postgres:14.0-alpine             "docker-entrypoint.s…"   39 minutes ago   Up 39 minutes   5432/tcp                                    sp_ask_admin_dashboard-master_db_1
24adadf28ba2   redis:6-alpine                   "docker-entrypoint.s…"   39 minutes ago   Up 39 minutes   6379/tcp                                    sp_ask_admin_dashboard-master_redis_1

When I login into my webapp container (ask_webapp). I tried this solution to find if am able to connect to Redis through "python manage.py shell" inside my container. And I am getting this error:

"OperationalError: Error -3 connecting to redis:6379. Temporary failure in name resolution."

My settings.py includes:

CELERY_BROKER_URL = "redis://redis:6379/0"
CELERY_RESULT_BACKEND = "redis://redis:6379/0"

This info above is also on the .env.dev file

My docker-compose.yml look as follow:

  web:
    container_name: ask_dashboard_web
    build:
      context: .
      dockerfile: ./sp_dashboard/DockerfileBuild
    image: ask_webapp
    restart: always
    command: >
      sh -c "python sp_dashboard/manage.py collectstatic --noinput &&
             python sp_dashboard/manage.py wait_for_db &&
             python sp_dashboard/manage.py migrate &&
             python sp_dashboard/manage.py runserver 0.0.0.0:8000"    
    volumes:
      - .:/usr/src/app/
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev
    networks:
      - djangonetwork
    environment:
      - POSTGRES_USER=hello_django
      - ...
    depends_on:
      - db

  redis:
    image: redis:6-alpine

  celery_worker:
    build:
      context: .
      dockerfile: ./sp_dashboard/DockerfileBuild
    image: sp_ask_dashboard_celery_worker
    command: >
      sh -c "cd sp_dashboard ; celery -A settings worker -l INFO"
    volumes:
      - .:/usr/src/app/
    env_file:
      - ./.env.dev
    environment:
      - POSTGRES_USER=hello_django
      - POSTGRES_PASSWORD=hello_django
      - POSTGRES_DB=hello_django_dev
      - "POSTGRES_HOST_AUTH_METHOD=trust"
    depends_on:
      - redis
      - db
      - web

CodePudding user response:

Your redis container in docker-compose.yml is not in djangonetwork it should look like something like this. You should apply the same logic to the Celery worker if it needs to connect to redis.

  redis:
    image: redis:6-alpine
    networks:
      - djangonetwork

Explanation: Inernal docker DNS only works in user define networks. You cannot call another container in different user defined network via Name.

  • Related