简体   繁体   中英

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

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

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.

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