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
Related
I want to connect my MongoDB docker with my program in my host.
I try this:
docker pull mongo
docker run -d --name mongodb -p 21017:21017 mongo
docker exec -it mongodb bash
All start fine but I couldn't connect to my host, I try to change my archive /etc/mongod.conf but with anything result.
I have a python program in my host and I want to use docker MongoDB and connect both.
docker container ls
Thank you very much.
That isnt the mongo port 21017:
Mongo port is 27017 .
You need to use below host & port in your python program which resides on docker host -
DB_HOST = localhost
DB_PORT = 27017
From your Docker host, mongoDB container should be accessible at localhost: 27017
Update 1(as suggested by #Schwarz54) -
Also, you are using wrong mongo port, it should be 27017.
Run your container using below command -
docker run -d --name mongodb -p 27017:27017 mongo
I find the problem, it was that in my python program I don't remember to import mongo.
Yes, that was the problem.
Now I can:
myclient = pymongo.MongoClient("mongodb://192.168.10.170:55059")
And all fine.
Thank you for the help I stay two days checking all the code but I don't remember to see if I import the library...
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
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
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
I'm running a NodeJS App with docker-compose. Everything works fine and I can see all my data by connecting to Mongo inside container. But when I connect to RoboMongo I don't see any data.
How can I deal with this problem?
There is another way. You can
SSH with Robomongo into your actual virtual server that hosts your docker applications (SSH tab, check "Use SSH tunnel" and complete the other fields accordingly)
Now ssh into the same machine in your terminal.
docker ps should show you your MongoDB container.
docker inspect <mongo container id> will print out complete information about that container. Look for IPAddress in the end, that will give you the local IP of the container.
In the "Connection" tab in Robomongo use that container IP to connect.
Another sidenote: Make sure that you don't expose your mongodb service ports in any way (neither Dockerfile nor docker-compose.yml), cause that will make your database openly accessible from everywhere. Assuming that you don't have set up a username / password for that service you will be scanned and hacked soon.
The easiest way is to enable forwarding the Mongo Container itself, here's how my docker-compose looks like.
mongo:
image: mongo
restart: always
ports:
- 27017:27017
You should do a Robomongo SSH tunnel connection to MongoDB inside docker container. First of all you should install a ssh server inside your docker container.
https://docs.docker.com/engine/examples/running_ssh_service/
After that you should configure your connection in Robomongo.
Inside "Connection Settings" there are configuration tabs of your Robomongo Connection.
Go to "SSH" Tab and configure your SSH connection to the docker container. After that go to "Connection" Tab and configure your connection to MongoDB as if it was in localhost scope.
I was facing a different problem. I had installed MongoDB locally. So, when the MongoDB on docker was running, it was clashing with the one running on my host. I had installed it using brew.
So, I ran
brew services stop mongodb-community
and then I restarted Robo3t. I saw the databases created in the MongoDB running on the docker.
Voila!
Please note that maybe you won't be able to use ssh because it was just a problem of incompatibility between mongo and robomongo.
'Robomongo v8.5 and lower doesn't support MongoDB 3'. It has nothing to do with docker.
First log in with ssh Login details
ssh -i yourpemfile.pem username#ipaddress
Check running container id for MongoDB
docker ps -a
then check the mongo container id
docker inspect container_id
Then open robo3t
create new connection and add container id
use ssh login details to connect to mongodb
In your docker-compose file, you can expose a port to the host.
For example, the following code will expose port 27017 inside the machine to the port 27018 in the host.
app:
image: node
volumes:
- /app
ports:
- "27018:27017"
Then, if you have docker-machine installed and your machine is default, you can do in a terminal :
docker-machine ip default
It will give you the ip of your host, for example 192.168.2.3. The address of your database (host) will be 192.168.2.3 and the port 27018.
If your docker machine is not virtual and is your OS, the address of your database will be localhost and the port 27018.