2

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

Axelly
  • 589
  • 3
  • 7
  • 26
  • I am usually unlucky trying to connect from `windows` to anything listening inside `wls-2`. `wls-2` to `wls-2` works OK. – usuario Jan 14 '22 at 12:43
  • that's not good :P – Axelly Jan 14 '22 at 14:56
  • You'll find lots of similar issues elsewhere with solutions involving the `netsh interface portproxy` command. Sure, the port is exposed to the hypervisor, but you still need to expose the hypervisor ports to your windows host – OneCricketeer Jan 15 '22 at 04:30
  • @OneCricketeer idk, tried various combinations of those and nothing works for me – Axelly Jan 16 '22 at 19:19
  • Can you please edit the post to include what you've tried? Have you also tried not using a port range and just setting `27017:27017`? Or maybe it's a bug in Docker Compose and using `docker run -p 27017:27017` could have different behavior like your previous question? Do other container's ports allow you to connect? Do you need sudo? Or have you tried running docker command from CMD / powershell? – OneCricketeer Jan 16 '22 at 19:23
  • @OneCricketeer What do you mean by `Or have you tried running docker command from CMD / powershell?`? btw. I edited – Axelly Jan 16 '22 at 20:28
  • 1
    Have you tried defining the ports with the localhost (`'127.0.0.1:27017-27019:27017-27019'`) in your `docker-compose` file? – zoot Jan 16 '22 at 22:56
  • @zoot unfortunely network's still unreachable :( – Axelly Jan 22 '22 at 23:31
  • I think I might've found out what's going on. The problem should not be docker, but accessing the WSL2 localhost. I'd suggest trying `'127.0.0.1:27017-27019:27017-27019'` and `'27017-27019:27017-27019' in combination with this post [Access a localhost running in Windows from inside WSL2?](https://stackoverflow.com/a/69407064/7934282). I believe one of the configuration will work. – zoot Jan 23 '22 at 00:34

4 Answers4

4

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

mpromonet
  • 11,326
  • 43
  • 62
  • 91
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/
Lupidon
  • 599
  • 2
  • 17
0

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
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Hi again, but I have docker on my Linux [wsl2] guest not Windows host. – Axelly Jan 17 '22 at 18:21
  • @Axe Any reason why? If you don't install Docker for Windows and enable WSL2 integration (which will install Docker inside the WSL2 terminal as well), then the ports will not be forwarded outside the hypervisor automatically – OneCricketeer Jan 17 '22 at 20:16
  • Well, I thought that it's going to work just fine meanwhile I wouldn't have to install any additional thing on host – Axelly Jan 20 '22 at 19:04
  • @Axelly You could use `mongo-express`, as documented in the Mongo docker readme to access from a web UI, but I think you'd have the same problem that you need to forward the webserver port – OneCricketeer Jan 20 '22 at 20:41
0

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.