10

As part as an attempt to resolve a Hibernate OGM connection issue, I want to see if I connect from Robo 3T.

I build my MongoDB image and start it running.

docker ps:

MacBook-Pro:GoStopHandle NOTiFY$ docker ps CONTAINER ID IMAGE
COMMAND CREATED STATUS PORTS NAMES 0375a68b9988 gostophandlemongodb:latest
"docker-entrypoint.s…" 5 seconds ago Up 4 seconds
0.0.0.0:32844->27017/tcp goStopHandleMongo

The IP address of the containers is:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 0375a68b9988 172.17.0.2

I've enter the 'mongo' shell:

docker exec -it goStopHandleMongo mongo admin

I add a user & password:

> db.createUser(
...   {
...     user: "NOTiFY",
...     pwd: "MyPassword",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
...   }
... )
Successfully added user: {
    "user" : "NOTiFY",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        "readWriteAnyDatabase"
    ]
}

I then create a connection in Robo 3T: Robo 3T - create connection

I then set-up the 'Authentication':

Authentication

When I try & connect I get:

Connection dialog

Any suggestions?

NOTiFY
  • 969
  • 12
  • 24

3 Answers3

8

It wasn't until I had to reboot my Mac that I realised that I'd still had a local instance of MongoDB running, therefore the I wasn't connecting to my Docker instance of MongoDB but my local (Brew installed) instance.

Once I'd seen @Artjom Zabelin answer here and "Don't forget to map port to host port"

docker run --name some-mongo -p 27017:27017 -d mongo

It connected to MongoDB running my container.

@Frank Yucheng Gu was correct that I needed 'localhost'.

NOTiFY
  • 969
  • 12
  • 24
7

To connect, you should simply connect with localhost:27017 on Robo3T.

The problem originates in the confusion between a container's internal IP and its external IP. The internal IP is your container's address in the docker network, which is isolated from you host network (unless explicitly bridged). When you do the docker inspect command, you grabbed the container's IP on the docker network.

Your container is listening on 0.0.0.0:32844 in the docker network, and exposed to the host with a port mapping to port 27017. So if you have another container in the docker network, you should access your service with 172.17.0.2:32844. But if you have anything outside of the docker network, you should access your mongodb with either localhost:27017 or [your host IP]:27017.

Hope this helps!

Frank Yucheng Gu
  • 1,491
  • 3
  • 16
  • Thanks for that. Sure enough, I've stopped my 'localhost' version of MongoDB and I can connect using 127.0.0.1 in Robo 3T and I can run my local instance of JBoss WildFly which uses the Mongo DB. That's working too. So is my Docker container using the collections in the (original) 'data/db' folder? I thought I would have had to 'ADD' a folder in the Docker File? Am I correct in assuming that if I want to connect to the MongoDB Docker Container from my WildFly Docker Container I need to use '172.17.0.2:32844'? TIA – NOTiFY Apr 05 '19 at 15:20
  • Hard to say without seeing your Dockerfile and the docker run commands regarding the folder usage. Yes, you are correct that you can use the IP:Port to connect to your MongoDB from another container. However, if you are running multiple container services, you should consider using docker-compose to orchestrate them locally. The issue is that the IP address is subject to change and may cause issues down the line. If you created your stack with docker-compose, you can refer to your container using the service name and port as opposed to the non-persistent IP. – Frank Yucheng Gu Apr 05 '19 at 15:45
  • Many thanks. Fairly new to creating Docker Containers. Will look at docker-compose. – NOTiFY Apr 05 '19 at 15:47
  • thanks to this answer. I've beens scratching my head why i can't connect to it using robo3t. In my case, my local mongo server is running at default 27017 so i port forward it to 27018:27017. So i was able to connect to localhost:27018 in robo3t – The.Wolfgang.Grimmer Oct 07 '20 at 05:08
0

In my case, I was running docker on Mac OSX and docker was using my host IP address, and as a result docker inspect CONTAINER_ID --format '{{ .NetworkSettings.IPAddress }}' would give me an empty string. Docker host address on Mac OSX and Windows is host.docker.internal. So I was able to connect to the mongodb instance running in docker after setting the host address on robo 3t to host.docker.internal

lomse
  • 3,322
  • 6
  • 37
  • 62