Connect to container's localhost interface in Docker - mongodb

Is it possible to connect to a process running in a Docker container but exactly via container's loopback interface ?
Basically I am looking for this option:
docker run ... -p 12345:127.0.0.1:12345 ...
This is rejected by Docker.
The use case is for example to setup Mongo admin user via Localhost Exception

You have the port mapping slightly wrong. Try it like this:
$ docker run -itd -p 27117:27017 mongo
so 27117 is the port you can connect to outwith the docker container, and 27017 is the port mongod is running on within docker
So when I connect the mongo shell I can connect to the mongod within docker like:
$ mongo --port 27117

Try like this :
Step 1 :
pulling mongo image from docker.
docker pull mongo
Step 2 :
Create new directory for storing mongodb data in you shared location like /home/user/databases/mongo
Step 3:
Run mongodb from docker image using below command
if you want change the mongodb port like 12345 set it in the command. 27017 is globally declared in docker you cannot change it but i want locally change the mongodb port like 12345
docker run -d -p 12345:27017 -v /home/user/databases/mongo:/data/db --name mongodb mongo

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

I want to connect my MongoDB docker to my host pc

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...

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

Unable to connect to mongoDB running in docker container

Following this example: https://docs.docker.com/engine/examples/mongodb/
When trying to connect to mongoDB with: mongo ip:27017
(where ip is the name from boot2docker ip) + the port number from docker ps:
27017/tcp
or with -P
0.0.0.0:49155->27017/tcp
Either way I get the following errors:
warning: Failed to connect to ip:27017, reason: errno:61 Connection
refused
Error: couldn't connect to server ip:27017 (ip), connection attempt
failed at src/mongo/shell/mongo.js:148 exception: connect failed
If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb, which is defined in your docker-compose.yml :
services:
mongodb_service:
image: mongo
I was using the hostname value and that's not the correct thing to do. You could verify this by looking at docker inspect mongodb_service in the Aliases section.
I was using port 27017 instead of 49155 (doh, port forwarding)
0.0.0.0:49155->27017/tcp
Thanks to ZeissS
If you are on a Mac and using Docker Machine, do the following:
1. Get the name of the VM running docker daemon
$ docker-machine ls
2. Get the VM's IP info
$ docker-machine env
3. Connect with the mongo client to the VM IP and the mongo mapped port
$ mongo VM-IP:port
Assuming your mongodb is within a container, for other containers to connect to it, they all need to be on the same network.
To have mongodb and other containers (that want to connect it), create a new network using below command
docker network create --driver bridge my_bridge
Then run mongodb and other containers using the --net flag
docker run --net=my_bridge --name mongodb -p 27017:27017 mongodb
docker run --net=my_bridge --name my-service -p 7002:7002 my-service
Now you should be able to connect mongodb with given alias name from those containers
mongo --host "mongodb:27017"
DATABASE_URI=mongodb://mongo:27017/db_name
Should be the Database URI for a service definition like below (and not mongodb://localhost
or mongodb://IP). Use service name or container name.
services
mongo:
container_name: mongo
image: mongo
ports:
- '27017:27017'