如何在停机时将redis服务器与django连接起来?

时间:2018-01-30 06:50:46

标签: django docker redis

目前我正在开发django app并尝试使用reddis服务器。我在settings.py中添加了reddis服务器的所有配置设置,我的设置是这样的。

    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",
    },
}

当我跑

时它的工作正常
python manage.py runser or python manage.py runworkers

但是当我对这个django应用程序进行dockerize时,它不会与reddis服务器建立连接。它给出了以下错误。

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

我的dockerfile就是这个。

# 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!

我也试过了。

# 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!

但这并不是在容纳我的django应用程序时建立连接。

任何人都可以告诉我,我错在哪里?我如何将我的django应用程序停靠,该应用程序将与设置在settings.py中的位置的redis服务器建立连接。 任何帮助或建议将受到高度赞赏。 感谢。

0 个答案:

没有答案