【问题标题】:Connection refused when using redis with docker-compose使用带有 docker-compose 的 redis 时连接被拒绝
【发布时间】:2019-12-10 04:01:55
【问题描述】:

这是我当前的 docker-compose.yml:

version: "2.0"
services:
    redis:
      image: redis
      container_name: framework-redis
      ports:
        - "127.0.0.1:6379:6379"
    web:
      image: myContainer:v1
      container_name: framework-web
      depends_on:
        - redis
      volumes:
        - /var/www/myApp:/app
      environment:
        LOG_STDOUT: /var/log/docker.access.log
        LOG_STDERR: /var/log/docker.error.log
      ports:
        - "8100:80"

我尝试了不同的设置;例如:不使用redis的端口值,使用0.0.0.0,切换到expose选项。

如果我尝试从主机使用 127.0.0.1 进行连接,它可以工作,但它会失败,并为我的应用容器显示 connection denied 消息。

有什么想法吗?

【问题讨论】:

  • 这个"127.0.0.1:6379:6379"不应该只是"6379:6379"吗?
  • 你是如何从应用容器连接到 Redis 的?您应该使用 Compose 文件中的服务名称作为主机名,在您的情况下为连接字符串中的 redis

标签: docker redis connection


【解决方案1】:

如果您从framework-web 访问framework-redis,则需要使用ip(或容器名称,即framework-redis)和framework-redis 的端口来访问它。由于它将位于 docker 桥后面,172.17.0.0/16 范围内的 ip 将分配给framework-redis。您可以使用该 IP,或者最好只提供容器名称以及 6379 端口。

 $ cat docker-compose.yml
version: "2.0"
services:
    redis:
      image: redis
      container_name: framework-redis
    web:
      image: redis
      container_name: framework-web
      depends_on:
        - redis
      command: [ "redis-cli", "-h", "framework-redis", "ping" ]
  $
  $ docker-compose up
Recreating framework-redis ... done
Recreating framework-web   ... done
Attaching to framework-redis, framework-web
framework-redis | 1:C 09 Dec 2019 19:25:52.798 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
framework-redis | 1:C 09 Dec 2019 19:25:52.798 # Redis version=5.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
framework-redis | 1:C 09 Dec 2019 19:25:52.798 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
framework-redis | 1:M 09 Dec 2019 19:25:52.799 * Running mode=standalone, port=6379.
framework-redis | 1:M 09 Dec 2019 19:25:52.800 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
framework-redis | 1:M 09 Dec 2019 19:25:52.800 # Server initialized
framework-redis | 1:M 09 Dec 2019 19:25:52.800 # 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.
framework-redis | 1:M 09 Dec 2019 19:25:52.800 * DB loaded from disk: 0.000 seconds
framework-redis | 1:M 09 Dec 2019 19:25:52.800 * Ready to accept connections
framework-web | PONG
framework-web exited with code 0

正如您在上面看到的,我收到了PONGPING 命令。

补充几点:

  • portsHOST_PORT:CONTAINER_PORT 的形式编写。您不需要提供 IP(正如 @coulburton 在 cmets 中指出的那样)。

  • 如果您只是从framework-web 访问framework-redis,那么您不需要发布端口(即,端口部分中的6379:6379)。当我们想从其他网络(例如,主机或其他物理机)访问容器网络中运行的应用程序(据我所知默认为172.17.0.0/16)时,我们只需要发布端口。

【讨论】:

    猜你喜欢
    • 2017-05-09
    • 2017-07-10
    • 2016-02-14
    • 2021-10-12
    • 2019-01-22
    • 2021-08-29
    • 2021-06-19
    • 2016-03-08
    • 2017-08-05
    相关资源
    最近更新 更多