简体   繁体   中英

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

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

"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:

  1. GO to the terminal
  2. connect to the container by docker exec -it MongoDB bash
  3. call to mongodb

If you like to use mongodb and to see it in fancy GUI I recommend using 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.
  2. Open Windows Powershell , not WSL2 terminal (I think this is your problem, by the fact that you're using sudo )
  3. Run docker compose up (which uses the newer Compose V2)
  4. Run netstat -ab in the Powershell session to see 0.0.0.0:27017 is indeed listening on the windows host
  5. Successfully connect with Robo 3T with the username/password on localhost:27017

在此处输入图像描述

Using Docker for Windows with WSL2 Integration enabled

在此处输入图像描述

在此处输入图像描述

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 .

While I never used Robo 3T this solved the issue for me when I ran into the same problem with mongocompass.

As already mentioned you might want to use docker directly in windows and run docker-compose up in powershell.

To connect from window host to linux WSL2, you could use localhost, or when it fails the WSL2 ip.
You can get it using from WSL2 with hostname -I | cut -f 1 -d ' ' hostname -I | cut -f 1 -d ' '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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