简体   繁体   English

docker-compose + django + redis-错误111连接到127.0.0.1:6379。 拒绝连接

[英]docker-compose + django + redis - Error 111 connecting to 127.0.0.1:6379. Connection refused

I've looked through this answer and can't figure out how to apply it to my problem, so if the answer is there, clarification would be appreciated. 我已经仔细阅读了此答案 ,却无法弄清楚如何将其应用于我的问题,因此,如果有答案,请务必进行澄清。

I'm also a noob to docker and docker-compose. 我也是docker和docker-compose的菜鸟。

I have a simple docker-compose.yml 我有一个简单的docker-compose.yml

version: '3'

services:
  redis:
    image: "redis:alpine"
  web:
    build: . # current directory
    command: bash -c "python /app/src/manage.py migrate && 
                      python /app/src/manage.py runserver 0.0.0.0:8000"
    volumes: 
      - .:/app
    ports:
      - "8000:8000"

When I run this with: docker-compose up everything seems ok: 当我运行这个具有: docker-compose up一切似乎确定:

$ docker-compose up
Starting hackerspace_redis_1 ... 
Starting hackerspace_redis_1 ... done
Starting hackerspace_web_1 ... 
Starting hackerspace_web_1 ... done
Attaching to hackerspace_redis_1, hackerspace_web_1
redis_1  | 1:C 19 Jul 2019 16:49:10.644 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 19 Jul 2019 16:49:10.644 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 19 Jul 2019 16:49:10.644 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 19 Jul 2019 16:49:10.645 * Running mode=standalone, port=6379.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # Server initialized
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1  | 1:M 19 Jul 2019 16:49:10.645 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 19 Jul 2019 16:49:10.645 * Ready to accept connections
web_1    | Operations to perform:
web_1    |   Apply all migrations: account, admin, announcements, auth, badges, comments, contenttypes, courses, django_summernote, djconfig, djcytoscape, flatpages, notifications, portfolios, prerequisites, profile_manager, quest_manager, sessions, sites, socialaccount, suggestions, utilities
web_1    | Running migrations:
web_1    |   No migrations to apply.
web_1    | Performing system checks...
web_1    | 
web_1    | System check identified no issues (0 silenced).
web_1    | July 19, 2019 - 09:49:16
web_1    | Django version 2.0.13, using settings 'hackerspace_online.settings'
web_1    | Starting development server at http://0.0.0.0:8000/
web_1    | Quit the server with CONTROL-C.

And I can visit my django app through a browser at 127.0.0.0:8000. 我可以通过浏览器127.0.0.0:8000访问django应用程序。 However, when I try to log in to the app, I get: 但是,当我尝试登录该应用程序时,我得到:

ConnectionError at /accounts/login/ 位于/ accounts / login /的ConnectionError

Error 111 connecting to 127.0.0.1:6379. 错误111连接到127.0.0.1:6379。 Connection refused. 拒绝连接。

Here's how I'm connecting to redis in my django settings: 这是我在Django设置中连接到Redis的方法:

REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')
REDIS_PORT = os.environ.get('REDIS_PORT', '6379')

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://{}:{}/1".format(REDIS_HOST, REDIS_PORT),
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    #...
}

How do I get my django app to connect to the redis container? 如何让django应用连接到Redis容器?

Note that as far as I can tell, there is nothing else using port 6379 (via sudo lsof -i -P -n | grep LISTEN ) 请注意,据我所知,没有其他使用端口6379的端口(通过sudo lsof -i -P -n | grep LISTEN

127.0.0.1 in Docker almost always means "this container". Docker中的127.0.0.1几乎总是表示“此容器”。 If you're running your application under Docker Compose , it 如果您正在Docker Compose下运行应用程序

... sets up a single network for your app. ...为您的应用设置一个网络。 Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name. 为服务每个容器连接默认的网络,是由网络上的其他容器都可达 ,并在相同的容器名称主机名被他们发现

That is, within the context of this docker-compose.yml file, there are host names redis and web that point at the two containers. 也就是说,在此docker-compose.yml文件的上下文中,有主机名redisweb指向两个容器。

You've already done one of the important configuration things. 您已经完成了重要的配置工作之一。 When your code says 当您的代码说

REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1')

you can set an environment variable that overrides the built-in default value. 您可以设置一个覆盖内置默认值的环境变量。 So here you just need to add an environment variable setting to your docker-compose.yml file: 因此,这里您只需docker-compose.yml文件添加一个环境变量设置:

version: '3'
services:
  redis:
    image: "redis:alpine"
  web:
    build: . # current directory
    environment:
      - REDIS_HOST=redis
    ports:
      - "8000:8000"
    # application source code and default command are built into the image

If you are using docker-compose you need to refer to the name of the container (ie redis) and not localhost. 如果您使用的是docker-compose,则需要引用容器的名称(即redis),而不是localhost。 Localhost is per a container and docker-compose will connect each container on a implicit network for which they need to resolve the name. Localhost是每个容器,docker-compose将连接隐式网络上的每个容器,它们需要为其解析名称。 Think of them as separate machines. 将它们视为独立的机器。 It helps to get perspective when configuring the networking. 在配置网络时,它有助于获得视角。 So a quick adjustment for which you are trying to connect to redis should fix the problem. 因此,您尝试连接到Redis的快速调整应该可以解决该问题。

The documentation https://docs.docker.com/compose/networking/ describes how the default network is created when you don't create them yourself explicitly. 文档https://docs.docker.com/compose/networking/描述了当您没有自己明确创建默认网络时如何创建默认网络。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 上的 Redis:连接到 0.0.0.0:6379 时出现错误 111。 拒绝连接 - Redis on Docker: Error 111 connecting to 0.0.0.0:6379. Connection refused 无法连接到 redis://localhost:6379/0:连接到 localhost:6379 时出现错误 111。 拒绝连接 - Cannot connect to redis://localhost:6379/0: Error 111 connecting to localhost:6379. Connection refused 消费者:无法连接到 redis://localhost:6379//:连接到 127.0.0.1:6379 时出现错误 61。 连接被拒绝 - consumer: Cannot connect to redis://localhost:6379//: Error 61 connecting to 127.0.0.1:6379. Connection refused OperationalError,连接到 127.0.0.1:6379 的错误 111。 拒绝连接。 在 heroku 中部署后 - OperationalError, Error 111 connecting to 127.0.0.1:6379. Connection refused. After deploying in heroku redis正在运行,但我得到“错误111连接到localhost:6379。 拒绝连接” - redis is running but I'm getting “Error 111 connecting to localhost:6379. Connection refused” 连接到 localhost:6379 时出现错误 111。 连接被拒绝。 姜戈赫鲁库 - Error 111 connecting to localhost:6379. Connection refused. Django Heroku 引发 ConnectionError(self._error_message(e)) kombu.exceptions.OperationalError:连接到 localhost:6379 的错误 111。 拒绝连接 - raise ConnectionError(self._error_message(e)) kombu.exceptions.OperationalError: Error 111 connecting to localhost:6379. Connection refused Redis + Docker + Django - 错误111连接被拒绝 - Redis+Docker+Django - Error 111 Connection Refused redis.exceptions.ConnectionError:连接到 localhost:6379 时出现错误 97。 协议不支持的地址族 - redis.exceptions.ConnectionError: Error 97 connecting to localhost:6379. Address family not supported by protocol 芹菜拒绝Docker-compose连接 - Docker-compose connection refused from celery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM