Mongo DB using Docker Image - mongodb

Operating System :- windows 10
I am using Docker toolbox and Install MongoDB Image on my local system.
Image installation done successfully.
after that I am using command docker exec -it taxlien bash to connect with mongo db and it connected successfully but I am unable to connect through our local using MongoDB Compass Community and also through our web application.

Now Finally I got solution using cmd docker-machine ip default , I got IP address
after that I will execute cmd
docker run -p :27017 --name test -d mongo:3.6
then i will connect with my local application and MongoDB commuinty.

Try this docker-machine ip default, and replace localhost by the result
See How do I connect to a container hosted in Docker Toolbox?
And check this doc https://docs.docker.com/toolbox/toolbox_install_windows/#optional-add-shared-directories

Related

How to connect to MongoDB running in local machine from a docker container?

I have a docker container running a spring-boot application for which i plan to use the mongoDb in my local machine.I know that containers are on a different network, and have made the necessary changes in the /etc/mongod.conf file as suggested by https://tsmx.net/docker-local-mongodb/ , in order for mongodb to accept connections from the docker network. But still the connection times out when the connection attempt is made from the docker container. Any help is appreciated.
You need to check the network interfaces of your host. You should find one starting with 192.168 or similar. Make sure your MongoDb instance is listening on this interface.
When you run the container, add --add-host mongodb:192.168.X.X to the docker run command. Replace the IP you find at the previous point.
docker run --help | grep add-host
--add-host list Add a custom host-to-IP mapping (host:ip)
Now in your Spring Boot application you can look for your MongoDB server called mongodb.
`docker run -d --add-host=host.docker.internal:host-gateway --name xxx -p 4001:4000 xxx`
above command gives access of local host of server to docker container.
Now when you connect to mongodb from inside docker container access it like this
let uri = "mongodb://host.docker.internal:27017"
Here 27017 is default port of mongodb

Can authenticate to docker mongo instance with CLI, but not by any other means

I have a bitnami mongo image that i start using:
docker run -p 27017:27017 -it --name mongodb <myregistry>.azurecr.io/movo.mongodb
I seed the mongo database using a script from which the output can be seen on the left hand of the image.
The problem:
I can connect the database using the mongo-cli.
However, i can't authenticate using Robo3T or my C# solution, using an identical connectionstring..
This works:
docker exec -it mongodb mongo admin -u movoproto -p "...<MyPwd>..."
But i cannot authenticate in any other way.
The connection does not seem to be the problem...
I've got an identical setup on my laptop where it workes fine...
After some fiddling i figured out what the issue was in the end.
It appears mongodb on windows can start as a local service, which runs a database on 127.0.0.1:27017.
So using Robo3T I was connecting to this local instance instead of my mapped docker mongo instance.
With Robo3T i could connect to this local instance when i unticked 'Perform Authentication'.
In Robo3T you can choose -> Right-click 'Open shell' -> db.hostInfo() -> F5 -> View results in text mode.
This would give info about my desktop computer
Whereas docker exec -it mongodb mongo --eval 'db.hostInfo()' would display information regarding my docker image.
My solution was to disable the mongo service on my desktop pc that runs the local database.
Doing this would let docker run -p 27017:27017 -it --name mongodb <myregistry>.azurecr.io/movo.mongodb bind to my docker container at 127.0.0.1:27017

How to connect MongoDB docker by Studio 3T?

I am using docker image https://hub.docker.com/_/mongo/ (Latest MongoDB version)
I run command
docker run --name some-mongo -d mongo
Then I install Studio 3T
I enter connection information like this
but I can't connect. What is correct connection must declare in Studio 3T in this case? How to connect MongoDB instance (docker) by Studio 3T?
You need to export the port you want to use in your docker command. e.g.
docker run -p 127.0.0.1:27017:27017 --name some-mongo -d mongo
This opens the port of the container on your host machine.
Click New Connection
Enter the Connection name
Click on From URI
Enter the URI in the following format
mongodb://{username}:{password}#{ip_address}:{port}/?authSource=admin
Click OK
Click Test Connection
Works?
No: Check your username, password, etc
yes: Congrats!
You need to find the IP address where the Docker container is running. On Mac docker runs in the background inside a linux VM that has its own IP. Thus localhost will not work.
To find the IP, run docker-machine env default and set this IP in the Server field.
for those on windows kindly check in task manager and make sure you don't have a local installation of mongo db server running then use localhost in address/connection string
I was running mongodb with wsl2 and docker, so I needed just to add "from uri", and set up ip_adress with the ip from wsl2.
I used this URI:
mongodb://{username}:{password}#{ip_address}:{port}/?authSource=admin
username = MONGO_INITDB_ROOT_USERNAME
password = MONGO_INITDB_ROOT_PASSWORD
port = 27017(docker container port will be set up on docker parameter "-p")
ip_address = Ip from wsl2 in my case, or localhost if you running docker locally.
This was my command to run the container on the first time :
docker container run -d -e MONGO_INITDB_ROOT_USERNAME=mongouser -e MONGO_INITDB_ROOT_PASSWORD=mongopwd -p 27017:27017 -v mongo_vol:/data/db mongo:4.4.3

How to connect Mongo DB using Compass running as a container in Windows 10 env?

My docker is running in windows 10 env with IP address: 192.168.##.###
I started my mongodb as a container using command:
docker container run -p 28000:27017 --name mongodb --detach mongo:3.4
I successfully connected from docker env to mongo db using mongodb shell as follows:
docker exec -it mongodb mongo
I could not connect to mongodb instance using my windows IP address and port number 28000 from MongoDB compass.
How can I connect?
I executed following commands and found the IP address of my docker machine:
$docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v17.12.0-ce
$ docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
Then by passing the IP Address 192.168.99.100 and Port number 28000, I successfully connected to my Mongodb instance running in container.

How to allow remote connections from mongo docker container

I am using the official mongodb docker container.
I want to connect to the mongodb container from my host machine on port 27017.
I ran the container with these ports exposed
-p 27017:27017
I am not able to connect (connection refused) and I believe its because the mongo conf file is not configured to allow remote connections. How can I configure it to allow? The official container does not have vi/nano installed to modify the image.
I am able to connect to mongodb from another container by creating a link - however this is not my wish
Better solutions for furthering:
https://blog.madisonhub.org/setting-up-a-mongodb-server-with-auth-on-docker/
https://docs.mongodb.com/v2.6/tutorial/add-user-administrator/
My answer to another question. How to enable authentication on MongoDB through Docker?
Here's what I did for the same problem, and it worked.
Run the mongo docker instance on your server
docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo
Open bash on the running docker instance.
docker ps
CONTAINER IDIMAGE COMMAND CREATED STATUS PORTS NAMES
b07599e429fb mongo "docker-entrypoint..." 35 minutes ago Up 35 minutes 0.0.0.0:27017->27017/tcp musing_stallman
docker exec -it b07599e429fb bash
root#b07599e429fb:/#
Reference- https://github.com/arunoda/meteor-up-legacy/wiki/Accessing-the-running-Mongodb-docker-container-from-command-line-on-EC2
Enter the mongo shell by typing mongo.
root#b07599e429fb:/# mongo
For this example, I will set up a user named ian and give that user read & write access to the cool_db database.
> use cool_db
> db.createUser({
user: 'ian',
pwd: 'secretPassword',
roles: [{ role: 'readWrite', db:'cool_db'}]
})
Reference: https://ianlondon.github.io/blog/mongodb-auth/ (First point only)
Exit from mongod shell and bash.
Now run the mongo docker with auth enabled.
docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo mongod --auth
Reference: How to enable authentication on MongoDB through Docker? (Usman Ismail's answer to this question)
I was able to connect to the instance running on a Google Cloud server from my local windows laptop using the below command.
mongo <ip>:27017/cool_db -u ian -p secretPassword
Reference: how can I connect to a remote mongo server from Mac OS terminal