无法在主机模式下连接到 docker 容器(redis)

时间:2021-07-12 23:01:10

标签: docker redis

这可能只是一般的 WSL 相关,但 Redis 是我的用例。

这很好用,我可以像这样连接: docker exec -it redis-1 redis-cli -c -p 7001 -a Password123

但是我无法从我的本地 Windows pc 到容器建立任何连接。我得到

<块引用>

无法连接:连接到 host.docker.internal:7001 时出现错误 10061。由于目标机器主动拒绝,无法建立连接。

当容器没有运行时,这也是同样的错误,所以不确定是 docker 问题还是 WSL?

version: '3.9'
  
services:     
  redis-cluster:
    image: redis:latest
    container_name: redis-cluster
    command: redis-cli -a Password123 -p 7001 --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1 --cluster-yes
    depends_on:
      - redis-1
      - redis-2
      - redis-3
      - redis-4
      - redis-5
      - redis-6
    network_mode: host
      

  redis-1:
    image: "redis:latest"
    container_name: redis-1

    network_mode: host

    entrypoint: >
      redis-server
      --port 7001
      --appendonly yes
      --cluster-enabled yes
      --cluster-config-file nodes.conf
      --cluster-node-timeout 5000
      --masterauth Password123
      --requirepass Password123
      --bind 0.0.0.0
      --protected-mode no

# Five more the same as the above

1 个答案:

答案 0 :(得分:0)

根据提供的 docker-compose.yml 文件,容器端口未公开,因此无法从外部(您的 windows/wls 主机)访问它们。检查 here 以获取官方参考。详细了解 docker 和端口 here

作为 redis-1 服务的示例,您应该将以下内容添加到定义中。

...
redis-1:
  ports:
  - 7001:7001
  ...
...

docker exec ... 正在工作,因为可以从容器内部访问该端口。