gpt4 book ai didi

docker - 将 redis 与 docker-compose 一起使用时连接被拒绝

转载 作者:行者123 更新时间:2023-12-02 18:59:50 28 4
gpt4 key购买 nike

这是我当前的 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 进行连接,它可以工作,但会失败,并显示我的应用容器的连接被拒绝消息。

有什么想法吗?

最佳答案

如果您正在访问framework-redis来自framework-web ,那么您需要使用 ip (或容器名称,即 framework-redis )和 framework-redis 端口来访问它。因为,它将位于 docker 桥后面,IP 范围为 172.17.0.0/16将分配给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

正如您在上面看到的,我收到了 PONG对于PING命令。

一些补充要点:

  • ports写成 HOST_PORT:CONTAINER_PORT 的形式。您不需要提供 IP(正如@coulburton 在评论中指出的那样)。

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

关于docker - 将 redis 与 docker-compose 一起使用时连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59255062/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com