Redis is disconnecting in production when using in docker with Django channels

can someone explain what is this error is all about, I am new to Redis and first time I'm using it

my websocket connection is rejected by server in the production environment.

most of the time it is getting rejected, but one time I got the error like this


future: <Task finished name='Task-2137' coro=<Connection.disconnect() done, defined at /usr/local/lib/python3.11/site-packages/redis/asyncio/connection.py:720> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/redis/asyncio/connection.py", line 729, in disconnect
    self._writer.close()  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/streams.py", line 344, in close
    return self._transport.close()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 839, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 761, in call_soon
    self._check_closed()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 519, in _check_closed
    raise RuntimeError('Event loop is closed')

in the ubuntu server

redis error

my Dockerfile looks like this

FROM python:3.11.1

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        postgresql-client \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .

EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

my docker-compose.yaml looks like this

version: '3'

services:
  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=2221
      - POSTGRES_DB=social-network
    volumes:
      - db-data:/var/lib/postgresql/data/
    ports:
      - "5432"

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      - DATABASE_URL=postgres://postgres:2221@db:5432/social-network
      - REDIS_URL=redis://redis:6379/0

  redis:
    image: redis
    ports:
      - "6379"

volumes:
  db-data:

my nginx configuration files looks like this

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
server {
    server_name IP_address example.com ;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /staticfiles/ {
        root /home/ubuntu/social-network-django-server;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/_____; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/____; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name IP_address example.com ;
    return 404; # managed by Certbot


}

Back to Top