0

Currently i am working on django app and trying to work with reddis server. i have add all configuration settings for reddis server in settings.py my settings are like this.

    redis_host = os.environ.get('REDIS_HOST', 'my-ip-of-reddis-server')

# Channel layer definitions
# http://channels.readthedocs.org/en/latest/deploying.html#setting-up-a-channel-backend
CHANNEL_LAYERS = {
    "default": {
        # This example app uses the Redis channel layer implementation asgi_redis
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [(redis_host, 6380)],
        },
        "ROUTING": "multichat.routing.channel_routing",
    },
}

its working fine when i run

python manage.py runser or python manage.py runworkers

but when i dockerize this django app, it does not make connection with reddis server. it gives following error.

redis.exceptions.ConnectionError: Error -2 connecting to redis:6380. Name or service not known.

    2018-01-30 06:00:47,704 - ERROR - server - Error trying to receive messages: Error -2 connecting to redis:6380. Name or service not known.
    201

My dockerfile is this.

# FROM directive instructing base image to build upon
FROM python:3-onbuild
RUN apt-get update
ENV PYTHONUNBUFFERED 1
ENV REDIS_HOST "redis"
# COPY startup script into known file location in container
COPY start.sh /start.sh

# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
#RUN python manage.py runserver

RUN daphne -b 0.0.0.0 -p 8000 --ws-protocol "graphql-ws" --proxy-headers multichat.asgi:channel_layer
# CMD specifcies the command to execute to start the server running.
CMD ["/start.sh"]
# done!

and i have also tried this.

# FROM directive instructing base image to build upon
FROM python:3-onbuild
RUN apt-get update
ENV PYTHONUNBUFFERED 1
ENV REDIS_HOST "redis"
# COPY startup script into known file location in container
COPY start.sh /start.sh

# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000
RUN python manage.py runserver

RUN daphne -b 0.0.0.0 -p 8000 --ws-protocol "graphql-ws" --proxy-headers multichat.asgi:channel_layer
# CMD specifcies the command to execute to start the server running.
CMD ["/start.sh"]
# done!

But this is not making connection while containerizing my django app.

Can anybody please tell me, where i am wrong? how can i dockerize my django app that will make connection with redis server whose settings are places in settings.py. Any help or suggestion will be highly appreciated. Thanks.

ucode
  • 31
  • 1
  • 1
  • 5
Amad
  • 314
  • 1
  • 6
  • 25
  • How do you run your redis host? Is it a container? – vivekyad4v Jan 30 '18 at 06:53
  • no its not in container. it is running on my server. – Amad Jan 30 '18 at 07:00
  • its settings are stored in settings.py file and it runs fine when i run python manage.py runserver. – Amad Jan 30 '18 at 07:01
  • `Dockerfile` looks good. Only you need is to connect to `localhost`. Yhis [question](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) may help – Bukharov Sergey Jan 30 '18 at 07:04
  • But you can choose another way. You can connect to dockerized `redis`. It is simple and is is right docker way. You can use `docker-compose`. I can help you if you need any help with `docker-compose` – Bukharov Sergey Jan 30 '18 at 07:06
  • please help me in docker-compose way. i am using version 3 of docker-compose.yml – Amad Jan 30 '18 at 07:12

0 Answers0