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
Related
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.
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
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
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.
I want to run a node app that uses express and connects to a (boot2docker) docker mongo container.
When I first wrote the app, I was using a locally installed instance of mongodb, and the following config worked:
module.exports = {
env: 'development',
mongo: {
uri: 'mongodb://localhost/fullstack-dev'
}
};
And it ran as expected.
Now I'm trying to swap that out for a docker instance of mongo.
So, I've done the following steps to get mongo running:
$ docker pull mongo:latest
$ docker run -v "$(pwd)":/data --name mongo -d mongo mongod --smallfiles
$ docker ps
#my mongo instance is 442c2541fe1a
$ docker exec -it 442c2541fe1a bash
$ mongo
At this point, appears to be running in my command prompt.
Then, I tried to get the IP of boot2docker vm that runs docker on osx:
$ boot2docker ip
# the IP returned: 192.168.59.103
So, then, I went to swap out the old mongodb path in the nodejs config to the following:
module.exports = {
env: 'development',
mongo: {
uri: 'mongodb://192.168.59.103:27017/fullstack-dev'
}
};
and when I run the application, I get a connection error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [192.168.59.103:27017]
What is the proper config for connecting my MEAN stack application with a docker'ified mongodb instance?
You need to publish the appropriate port on the container with the -p argument i.e:
docker run -p 27017:27017 -v "$(pwd)":/data --name mongo -d mongo mongod --smallfiles
The will make port 27017 accessible on the host (the VM in your case) and forward traffic to port 27017 on the container.