无法连接到 WSL2 内 docker 内的 mongo

Cannot connect to mongo that's inside docker inside WSL2

我的设置有什么问题?

这是我的 docker-compose.yml,我从 sudo docker-compose up -d

开始

结果是这样的:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                  NAMES
7fa1ebd185d3        mongo               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        0.0.0.0:27017-27019->27017-27019/tcp   mongodb

version: "3.8"
services:
  mongodb:
    image: mongo
    container_name: mongodb
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root1234356asdas
      - MONGO_INITDB_ROOT_PASSWORD=324gdfgdfgasdas
    volumes:
      - /home/user/mongodb/database:/data/db
    ports:
      - '27017-27019:27017-27019'
    restart: unless-stopped

我尝试通过 localhost/127.0.0.1/0.0.0.0 使用 Robot 3T 连接到它,但它总是导致

“远程计算机拒绝连接”

提前致谢

编辑。

我试过那些:

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=127.0.0.1 connectport=27017 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=127.0.0.1 connectport=27017 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=127.0.0.1 connectport=27017 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=0.0.0.0 connectport=27017 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=0.0.0.0 connectport=27017 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=0.0.0.0 connectport=27017 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=127.0.0.1 connectport=27018 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=127.0.0.1 connectport=27018 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=127.0.0.1 connectport=27018 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=0.0.0.0 connectport=27018 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=0.0.0.0 connectport=27018 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27018 listenaddress=0.0.0.0 connectport=27018 connectaddress=172.15.0.1



netsh interface portproxy add v4tov4 listenport=27019 listenaddress=127.0.0.1 connectport=27019 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=127.0.0.1 connectport=27019 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=127.0.0.1 connectport=27019 connectaddress=172.15.0.1

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=0.0.0.0 connectport=27019 connectaddress=172.22.245.111

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=0.0.0.0 connectport=27019 connectaddress=172.17.0.1

netsh interface portproxy add v4tov4 listenport=27019 listenaddress=0.0.0.0 connectport=27019 connectaddress=172.15.0.1

编辑这个对我有用:

连接到来自 hostname -I | cut -f 1 -d ' ' 的 IP(在 WSL 中执行)

和mongo创建为:

docker-compose.yml

version: "3.7"
services:
  mongo:
    container_name: container-mongodb
    image: mongo:latest
    restart: always
    ports:
      - 27017:27017

    environment:
      MONGO_INITDB_ROOT_USERNAME: "jane"
      MONGO_INITDB_ROOT_PASSWORD: "secret"
      MONGO_INITDB_DATABASE: "mydb"

    volumes:
      - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
      

mongo-init.js

print("Started Adding the Users.");
db = db.getSiblingDB("admin");
db.createUser({
  user: "userx",
  pwd: "1234",
  roles: [{ role: "readWrite", db: "admin" }],
});
print("End Adding the User Roles.");

我无法重现问题,但这是我采取的步骤。

  1. 将您的撰写文件复制到 Windows 文件系统。
  2. 打开 Windows Powershell,而不是 WSL2 终端(我认为这是你的问题,因为你正在使用 sudo
  3. 运行 docker compose up(使用较新的 Compose V2)
  4. 运行 netstat -ab 在 Powershell 会话中看到 0.0.0.0:27017 确实在 windows 主机上监听
  5. localhost:27017
  6. 上通过username/password成功连接Robo 3T

在启用 WSL2 集成的情况下将 Docker 用于 Windows

PS C:\WINDOWS\system32> docker version
Client:
 Cloud integration: v1.0.22
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:44:07 2021
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:43:56 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

如果您想在没有花哨的 GUI 的情况下进行连接,您可以执行以下步骤:

  1. 前往航站楼
  2. 通过docker exec -it MongoDB bash
  3. 连接到容器
  4. 致电mongodb

如果您喜欢使用 mongodb 并希望在精美的 GUI 中查看它,我建议您使用 dbvear

https://dbeaver.com/databases/mongo/

尝试使用 host.docker.internal:27017 而不是 localhost:27017

虽然我从未使用过 Robo 3T,但当我 运行 遇到与 mongocompass 相同的问题时,这解决了我的问题。

如前所述,您可能希望在 windows 中直接使用 docker,在 powershell 中直接使用 运行 docker-compose up

要从 window 主机连接到 linux WSL2,您可以使用本地主机,或者当 WSL2 ip 失败时。
您可以使用 hostname -I | cut -f 1 -d ' '

从 WSL2 获取它