Docker [Errno 111] Connect call failed ('127.0.0.1', 6379)

前端 未结 4 711
[愿得一人]
[愿得一人] 2021-01-06 09:47

I am trying to follow the tutorial here https://channels.readthedocs.io/en/latest/tutorial/part_2.html and check if channel layer can communicate with Redis. The only differ

相关标签:
4条回答
  • 2021-01-06 10:06

    Try changing 127.0.0.1:6379 to redis:6379.

    Although Redis is running, your python container isn't able to communicate with it; this is because it's trying to connect to 127.0.0.1:6379, but from the container's perspective, there's nothing running there. This can be a bit frustrating to debug, but it's a bit easier if you keep in mind that containers get their own network namespace. As a result, python's localhost != redis's localhost != your host machine's localhost.

    Luckily, it's easy to connect containers that are sharing the same bridge, and by default, docker-compose creates a single bridge network and connects all your containers to them, providing the necessary DNS to allow them to discover one another. As a result, container-to-container communication works simply by using the service name.

    As a note, it's possible to run containers in the same namespace, and to run in them in the namespace of the host, via the --net=container:<container-id> or --net=host flag. This is especially useful for running debugging tools in a container and attaching them to the network namespace of either another container or the host, e.g. using netshoot to see what ports are listening within the container (exposed or not), docker run --rm -it --net container:test_web_1 nicolaka/netshoot netstat -tulpn.

    0 讨论(0)
  • 2021-01-06 10:12

    So I got stuck on this and none of the answers seemed to work for me.

    I found that docker would give the container its own IP address. to find the IP address in the command line I used;

    'docker ps' to get the container ID. 'docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}} * container-id*' which gave me the IP.

    This worked for;

    CHANNEL_LAYERS = {
        'default': {
            'BACKEND': 'channels_redis.core.RedisChannelLayer',
            'CONFIG': {
                "hosts": [('172.20.0.1', 6379)],
            },
        },
    }
    
    0 讨论(0)
  • 2021-01-06 10:14
    docker run -p 6379:6379 -d redis:5
    

    and then start the server

    0 讨论(0)
  • 2021-01-06 10:24

    i have the same issue, the problem was the version of my python and also of the channels. the tutorial woks perfectly with python=3.6, channels-redis 2.3.1 and daphne 2.0.2, channels =2.0. You can follow the channels documentation of the channels version you have install.

    0 讨论(0)
提交回复
热议问题