I’ve spent the entire day trying to resolve this issue, which concerns WebSocket channels. My app works perfectly on my local environment.
First, My app works perfectly on local.
this is my docker compose
version: '3.12'
services:
db:
image: postgres:16
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
POSTGRES_DB: test_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
redis:
image: redis:6
container_name: redis
ports:
- "6379:6379"
web:
build:
context: .
dockerfile: configs/docker/dev-server/Dockerfile
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
- redis
environment:
- DATABASE_NAME=test_db
- DATABASE_USER=postgres
- DATABASE_PASSWORD=postgres
- DATABASE_HOST=db
- DATABASE_PORT=5432
- REDIS_URL=redis://redis:6379/1
- REDIS_HOST=redis
- REDIS_PORT=6379
volumes:
postgres_data:
My problem is that when I run my app on docker, I will get errors like this
OSError: [Errno 99] Cannot assign requested address
During handling of the above exception, another exception occurred:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.
According to my research, my app shouldn’t access to localhost, but host name. I think my settings is no problem.
print("REDIS_URL" ,getenv('REDIS_URL', 'redis://localhost:6379/1'))
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
'hosts': [getenv('REDIS_URL', 'redis://localhost:6379/1')],
},
},
}
I could see REDIS_URL redis://redis:6379/1 is logged out. so the env is successfully set.
docker network inspect shows the containers are connected properly.
“Containers”: {
“4d9f98ab4682e241d1d9e54aea60cfb0165b9eeb1b80ca76f7027e6c656951d3”: {
“Name”: “redis”,
“EndpointID”: “4985260d4ff2e193c1bcf9eadde5ea36284e13e7e4fdcb5afdbeb696d46c730e”,
“MacAddress”: “02:42:ac:1b:00:02”,
“IPv4Address”: “172.27.0.2/16”,
“IPv6Address”: “”
},
“89804ff5d2e4283049cb5a74b137f176258f3866fa40b4ed6c6b6ea8f86e6abc”: {
“Name”: “django-db-1”,
“EndpointID”: “f140a1c30bb32b47baa439e93ce12fee6887796ed2d2d1b422cc4d3b89dcaf59”,
“MacAddress”: “02:42:ac:1b:00:03”,
“IPv4Address”: “172.27.0.3/16”,
“IPv6Address”: “”
},
“979133cb3153fd62d80f467417f6eb416dd0ae07ea230cab82065c04edde8417”: {
“Name”: “django-web-1”,
“EndpointID”: “7361a78454b6d4315b0d47ce808e3316d484c9acb92d7628b64b17a7d31da985”,
“MacAddress”: “02:42:ac:1b:00:04”,
“IPv4Address”: “172.27.0.4/16”,
“IPv6Address”: “”
}
},
I could say again, When I run only db and redis on docker containers, then run my app locally, it will work. so I suppose problem come from docker network.
Thank you for your help😀