Cannot connect to mongo that's inside docker inside WSL2

Viewed 943

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"

enter image description here enter image description here enter image description here

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

Edit this worked for me:

Connect to IP thats comes from hostname -I | cut -f 1 -d ' ' (executed inside WSL)

and mongo created as:

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.");

enter image description here

4 Answers

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 ' '

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

enter image description here

Using Docker for Windows with WSL2 Integration enabled

enter image description here

enter image description here

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.

Related