使用带有 docker-compose 的 redis 时连接被拒绝
Connection refused when using redis with docker-compose
所以这是我现在的 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,切换到公开选项。
如果我尝试使用主机上的 127.0.0.1 进行连接,它可以正常工作,但它会失败,并显示我的应用程序容器的 连接被拒绝 消息。
有什么想法吗?
如果您从 framework-web
访问 framework-redis
,那么您需要使用 ip(或容器名称,即 framework-redis
)和 [=11= 的端口来访问它].因为,它将位于 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
正如您在上面看到的,我收到了 PING
命令的 PONG
。
补充几点:
ports
写成HOST_PORT:CONTAINER_PORT
的形式。您不需要提供 IP(正如@coulburton 在评论中指出的那样)。
如果您只是从 framework-web
访问 framework-redis
,那么您不需要发布端口(即端口部分中的 6379:6379
) .当我们想从其他网络(例如主机或某些网络)访问容器网络中的应用程序 运行(据我所知默认为 172.17.0.0/16
)时,我们只需要发布端口其他物理机)。
所以这是我现在的 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,切换到公开选项。
如果我尝试使用主机上的 127.0.0.1 进行连接,它可以正常工作,但它会失败,并显示我的应用程序容器的 连接被拒绝 消息。
有什么想法吗?
如果您从 framework-web
访问 framework-redis
,那么您需要使用 ip(或容器名称,即 framework-redis
)和 [=11= 的端口来访问它].因为,它将位于 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
正如您在上面看到的,我收到了 PING
命令的 PONG
。
补充几点:
ports
写成HOST_PORT:CONTAINER_PORT
的形式。您不需要提供 IP(正如@coulburton 在评论中指出的那样)。如果您只是从
framework-web
访问framework-redis
,那么您不需要发布端口(即端口部分中的6379:6379
) .当我们想从其他网络(例如主机或某些网络)访问容器网络中的应用程序 运行(据我所知默认为172.17.0.0/16
)时,我们只需要发布端口其他物理机)。