Running MongoDB from Docker - mongodb

I just downloaded Docker and created the MongoDB container on it.
I have successfully created the container and it is working on the port 168.192.99.100:32728 on my local machine. I'm confused how should I run mongo on my machine?
For example if we create the SQL container we can connect to the port using MySQL Workbench. How do i do something similar for Mongo?

You have at least two options:
Running the mongo-client on your host-system (requires install of mongo-client):
mongo --host 168.192.99.100 --port 32728
see https://docs.mongodb.com/manual/reference/program/mongo/#bin.mongo
or maybe the simpler option of attaching to the docker-container with the running mongodb-server which already should have a mongo-client installed:
docker exec -it "name of container" mongo
https://docs.docker.com/engine/reference/commandline/exec/

Related

Mongo: Same port used in Local Mongo Instance and when exposed through docker

I recently started learning docker. I already had a local instance of Mongo Server running on my local machine, I used two ports 27017 and 27018 (replica set). When I was running mongo container on my local machine, I used the following command
docker run -d --rm --name mongodb -p 27017:27017 mongo
I assumed I would get an error that 27017 port is already in use in local machine, but I did not get any error and the container started. I am unable to understand why. When i used Mongo Compass to see which mongo is running on port 27107, it showed the one that was already installed on my local machine from earlier (recognized it by looking at DB names). It was only when i stopped local mongo instances, and reconnected compass to localhost:27017 I saw a new mongo instance (probably the one that was created using docker).
How did docker bind 27017 internally when it was already in use?

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

Mongo DB using Docker Image

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

Can't access to docker mongo without being root

I just installed docker and I could run a mongodb image in a container, without sudo. The port exposed is 27017.
I'm now trying to access it from a shell running on the host, but it works only with
sudo mongo --port 27017
I don't want to use sudo. Is it possible?
Is it possible?
yes, but i think you're not taking the right approach.
if you're running mongodb in a docker container, you shouldn't be running the mongo shell from your host. you should be running it from your container.
for example, if your docker container is named mydb, you can do this:
docker exec -it mydb mongo
this will execute the mongo shell in the mydb container and give you "interactive" and "terminal emulation" (the -it params) so you can work as if you had the mongo shell directly on your computer.

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