SanjuB
SanjuB

Reputation: 53

How to connect Mongo docker container to mongodb compass community installed on host

1. Pulled docker image

2. Started container by:

$ docker run mongo

3. Took the ip of container by:

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

4. Launched mongodb compass client on host and tried to connect to the ip

Upvotes: 2

Views: 6944

Answers (2)

Alex MacArthur
Alex MacArthur

Reputation: 2286

What kind of errors are you seeing when you attempt to connect? I'd start by locking down the IP like the previous answer suggests and try to take note of the specific errors being thrown. I've had authentication issues in the past, which weren't made very descriptive by Compass.

If you're still stuck, you could try the Docker Compose configuration I use:

https://github.com/alexmacarthur/local-docker-db/tree/master/mongo

After spinning up the container, you should be able to create databases with users. Ex:

use admin;
db.auth("root", "password");
use myDatabase;
db.createUser({user: "root", pwd: "password", roles:[{role: "readWrite" , db:"myDatabase"}]});

Once a DB and respective user are created, you should be able to access the DB through a string like this:

mongodb://user:password@localhost:27017/myDatabase

I don't know what kind of application you're building, but this might at least be able to help get you up & running for the time being.

Upvotes: 1

prisar
prisar

Reputation: 3195

If map the container port to host port like this, then you can directly connect with loopback(127.0.0.1) ip or localhost. So you do not need to check for container IP every time. This IP is going to change everytime you create mongo container.

sudo docker run -d -p 27017:27017 -v ~/data:/data/db mongo

Upvotes: 4

Related Questions