run mongo in docker container - mongodb

i want to run mongoDb with docker in windows 10 for nodejs Project .
i run this command :
docker run --name CMSStore -p 27011:27017 -v e:/data/mongo:/data/db mongo
but it stop in this stop and not run .
whats the problem ? how can i run mongo with save data in valume ????

Please try:
docker run -d -p 27017:27017 -v ~/data:/data/db mongo
Are you sure that 27011 is listening ? why you using it?
Default port are here :
https://docs.mongodb.com/manual/reference/default-mongodb-port/
27017, 27018, 27019.
To see if mongodb is listening on 27011, check this command on mongo shell:
db.runCommand({whatsmyuri : 1})
it will display IP and port numbers in use.

Related

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

Mongo docker image - unable to run on different port

There are a tons of question here on SO citing how this command alone
docker run --name mymongo --network bridge -p 27117:27117 -v "$PWD/db":/data/db -d mongo
should run mongo on port 27117.
However this doesn't work for me. The container runs, but the mongo is run on its default port alone (see output from container itself):
# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
# mongo --port 27117
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27117/
2018-11-20T17:26:09.345+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27117, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27117 :: caused by :: Connection refused :
What is going on?
Thanks a lot!
Inside your container mongo runs on its default port, which is 27017
So, you should modify your command and specify the port mapping like this: -p 27117:27017
The full command would be this:
docker run --name mymongo --network bridge -p 27117:27017 -v "$PWD/db":/data/db -d mongo
With that command you are telling docker that the port is 27117, but you also need to start mongo with that port.
To do so, just add --port 27117 at the end of your command:
docker run --name mymongo --network bridge -p 27117:27117 -v "$PWD/db":/data/db -d mongo --port 27117

Connect to container's localhost interface in Docker

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

How to start a mongodb shell in docker container?

To start the container, I am typing the following command:
sudo docker run -i -t -p 28000:27017 mongo:latest /usr/bin/mongod --smallfiles
But I want to open the shell in this container to type the mongo commands.
What command should I run to do the same?
You can run the interactive mongo shell by running the following command:
docker run -it -p 28000:27017 --name mongoContainer mongo:latest mongo
Otherwise, if your container is already running, you can use the exec command:
docker exec -it mongoContainer mongo
The thing that I struggled too but found a solution:
docker pull mongo
docker run --name CONTAINERNAME --restart=always -d -p 8080:8080 mongo mongod --auth
sudo docker exec -i -t CONTAINERNAME bash
mongo
use admin
db.createUser({user:"user", pwd:"password", roles:[{role:"root", db:"admin"}]})
exit && exit
Now you have created a running Docker container with everything you need. Now if you want to connect from any client with an admin user, just run this
mongo -u "user" -p "password" HOSTIP --authenticationDatabase "admin"
Extending and updating #vladzam answer and if your container is already running in docker, you can use the exec mongosh command with login and pass options like this:
docker exec -it database-dev mongosh -u "myLogin" -p "myPass"
Download the latest MongoDB Docker image from Docker Hub
sudo docker pull mongo
Now set up MongoDB container
docker run --name containername mongo
Interact with the database through the bash shell client
docker exec -it containername bash
Launch the MongoDB shell client
mongosh #now it is mongosh to access shell
It depends which version of MongoDB you're running.
Please see the differences here : The MongoDB Shell versus the Legacy mongo Shell.
For example, with Mongo 3 the executable was mongo :
$ docker run --rm mongo:3 which mongo mongosh mongod
/usr/bin/mongo
/usr/bin/mongod
With Mongo 5 both mongo and mongosh are present :
$ docker run --rm mongo:5 which mongo mongosh mongod
/usr/bin/mongo
/usr/bin/mongosh
/usr/bin/mongod
With Mongo 6 you can only use the newest mongosh :
$ docker run --rm mongo:6 which mongo mongosh mongod
/usr/bin/mongosh
/usr/bin/mongod
Now if you want to try it, run a MongoDB instance :
$ docker run -d -p 29000:27017 --name mongodb-6 mongo:6
Then connect a shell :
$ docker exec -it mongodb-6 mongosh
You'll get something like :
Current Mongosh Log ID: 632456e42dbc88aa0bfe612f
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.4
Using MongoDB: 6.0.1
Using Mongosh: 1.5.4
...
You can also pass options like :
$ docker exec -it mongodb-6 mongosh --version
docker exec -it mongoContainer mongosh
i tried mongo, mongod, and mongodb but failed. Then i changed it to mongosh and it works!
Assuming you have mongo installed on your host computer, which was in my case when I asked this question years ago. This is an alternate way I tried: Open a new terminal
mongo 127.0.0.1:28000
Your mongo shell starts in this terminal now.

Docker - how to run mongodb process as daemon

I am running docker on windows with the docker-machine and Docker Toolbox installation package.
Boot2Docker is now deprecated, btw.
The docs here: https://docs.docker.com/examples/mongodb/ told me to connect to the running mongodb container like:
$ mongo --port 27017 --host 192.168.99.100
But I get this error:
$ mongo --host 192.168.99.100
sh: mongo: command not found
Any ideas?
EDIT 1: I run the container like this:
$ docker run -p 27017:27017 --name mongodb -d myname/repo
I guess instead of building your own myname/repo image of mongo you could have an easier start with the official mongo image: https://hub.docker.com/_/mongo/
Update as for the error you see, it looks like the mongo client is not installed at the wherever you execute your test. You could install it or use the mongo container: docker run -it --rm --link <id of the running mongo container>:mongo mongo mongo --host mongo