Cannot assign requested address when connecting to Redis through Docker
Creating containers from my Django+Redis+React project and getting the error:
Error 99 connecting to localhost:49157. Cannot assign requested address.
When I visit the url localhost:8000
This is my docker-compose file:
version: "3.8"
services:
redis:
restart: always
image: redis:latest
ports:
- "6379:6379"
pairprogramming_be:
restart: always
depends_on:
- redis
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
env_file:
- ./signup/.env
- ./payments/.env
- ./.env
build:
context: ./
dockerfile: Dockerfile
ports:
- "8000:8000"
container_name: "pairprogramming_be"
...
#Error 99 connecting to localhost:49153. Cannot assign requested address.
This my .env file:
DEBUG=1
DJANGO_ALLOWED_HOSTS=0.0.0.0
my Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR django-project/peerplatform
COPY requirements.txt ./
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . ./
EXPOSE 8000
CMD ["python", "./manage.py", "runserver", "0.0.0.0:8000", "--settings=signup.settings"]
Here is my settings.py incase the issue is here:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://redis:6379",
# "TIMEOUT": 5 * 60,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "pairprogramming"
}
}
...
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"LOCATION": "redis://redis:6379",
},
}
...
REDIS_HOST = 'redis://redis:6379'
REDIS_PORT = 6379
REDIS_PASSWORD = 'redispw'
I'm running into the same problem, in a Docker container with a similar stack. Haven't had the time to resolve it yet, but in my search for an answer I found this looking promising: https://medium.com/it-dead-inside/docker-containers-and-localhost-cannot-assign-requested-address-6ac7bc0d042b