简体   繁体   English

无法连接到 WSL2 内部 docker 内部的 mongo

[英]Cannot connect to mongo that's inside docker inside WSL2

What's wrong with my settings?我的设置有什么问题?

Here's my docker-compose.yml that I start with sudo docker-compose up -d这是我的docker-compose.yml ,我从sudo docker-compose up -d开始

which ends up like that:最终是这样的:

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

I tried connecting to it using Robot 3T via localhost/127.0.0.1/0.0.0.0 but it always results in我尝试通过 localhost/127.0.0.1/0.0.0.0 使用 Robot 3T 连接到它,但它总是导致

"remote computer refusing to connect" “远程计算机拒绝连接”

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

Thanks in advance提前致谢

edit.编辑。

I tried those:我试过那些:

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

If you want to connect without a fancy GUI you could do the next steps:如果您想在没有花哨的 GUI 的情况下进行连接,您可以执行以下步骤:

  1. GO to the terminal GO 到终端
  2. connect to the container by docker exec -it MongoDB bash通过docker exec -it MongoDB bash连接到容器
  3. call to mongodb致电mongodb

If you like to use mongodb and to see it in fancy GUI I recommend using dbvear如果您喜欢使用 mongodb 并在精美的 GUI 中查看它,我建议使用 dbvear

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

I cannot reproduce the problem, but here are the steps I took.我无法重现该问题,但这是我采取的步骤。

  1. Copy your compose file into Windows filesystem.将您的撰写文件复制到 Windows 文件系统中。
  2. Open Windows Powershell , not WSL2 terminal (I think this is your problem, by the fact that you're using sudo )打开Windows Powershell ,而不是 WSL2 终端(我认为这是您的问题,因为您使用的是sudo
  3. Run docker compose up (which uses the newer Compose V2)运行docker compose up (使用较新的 Compose V2)
  4. Run netstat -ab in the Powershell session to see 0.0.0.0:27017 is indeed listening on the windows host在 Powershell session 中运行netstat -ab可以看到0.0.0.0:27017确实在 windows 主机上监听
  5. Successfully connect with Robo 3T with the username/password on localhost:27017使用localhost:27017上的用户名/密码成功连接 Robo 3T

在此处输入图像描述

Using Docker for Windows with WSL2 Integration enabled将 Docker 用于 Windows 并启用 WSL2 集成

在此处输入图像描述

在此处输入图像描述

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

Try using Using host.docker.internal:27017 instead of localhost:27017 .尝试使用 Using host.docker.internal:27017而不是localhost:27017

While I never used Robo 3T this solved the issue for me when I ran into the same problem with mongocompass.虽然我从未使用过 Robo 3T,但当我遇到与 mongocompass 相同的问题时,这为我解决了这个问题。

As already mentioned you might want to use docker directly in windows and run docker-compose up in powershell.如前所述,您可能希望直接在 windows 中使用 docker 并在 Z2F2D399F0EA8047483BZZFE551 中运行docker-compose up

To connect from window host to linux WSL2, you could use localhost, or when it fails the WSL2 ip.要从 window 主机连接到 linux WSL2,您可以使用 localhost,或者当 WSL2 ip 失败时。
You can get it using from WSL2 with hostname -I | cut -f 1 -d ' '您可以使用hostname -I | cut -f 1 -d ' '从 WSL2 获取它。 hostname -I | cut -f 1 -d ' '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM