How to start a mongodb shell in docker container? - mongodb

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.

Related

MongoDB docker image connection issue

Im try to run a local docker container running MongoDB but I got some issues.
Im using MacOS and this is my docker file:
FROM mongo:4.4.10
RUN apt-get update && \
apt-get install -y mongodb-database-tools
ENV MONGO_INITDB_ROOT_USERNAME admin
ENV MONGO_INITDB_ROOT_PASSWORD admin
EXPOSE 27017
CMD ["mongod", "--bind_ip_all"]
But when I try to connect in mongo inside the container I got:
root#1820984d904c:/# mongo
MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6
exception: connect failed
exiting with code 1
Im running the container like this:
docker run -it -p 27017:27017 --rm --name containername imagename bash
Any ideas?
Thanks
You are not starting mongo with your docker run command. You are starting bash instead.
From https://docs.docker.com/engine/reference/run/#overriding-dockerfile-image-defaults:
CMD (default command or options)
Recall the optional COMMAND in the Docker commandline:
docker run [OPTIONS] IMAGE[:TAG|#DIGEST] [COMMAND] [ARG...]
This command is optional because the person who created the IMAGE may have already provided a default COMMAND using the Dockerfile CMD instruction. As the operator (the person running a container from the image), you can override that CMD instruction just by specifying a new COMMAND.
So start your docker with
docker run -p 27017:27017 --rm --name containername imagename
Then bash in to the container with
docker exec -it containername bash

MongoDB Backup and Docker Problems

I am weeks trying to do backup from my docker without any success.
My Docker Compose:
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: user
MONGO_INITDB_ROOT_PASSWORD: password
networks:
- internal
volumes:
- /opt/rpg/mongo/data:/data/db
Input: docker exec mongo sh -c 'exec mongodump -d rpg --archive' > /home/rpg/all-collections.archive
Output: Failed: error getting collections for database rpg: error running listCollections. Database: rpg Err: command listCollections requires authentication
Then i tried with password
Input: docker exec mongo sh -c 'exec mongodump -d rpg —uri="mongodb://user:password#mongo:27017/rpg?authSource=admin" --archive' > /home/rpg/all-collections.archive
Output: SASL authentication step: Authentication failed.
After weeks trying i get the connection with:
sudo docker run -it --rm --network internal mongo \
mongo --host mongo \
-u user \
-p password \
--authenticationDatabase admin \
rpg
Now I can see the collections and everything, but i still cant get the backup.
Tried too:
Input:
sudo docker run --rm --network internal mongo \
mongodump --host mongo \
-u user \
-p password \
--authenticationDatabase admin \
--db rpg
> ~/rpg2-collections.archive
Output:
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
Without success, can someone help me ?
Was having the same Authentication problem, and this command worked:
docker exec <docker_container_name> sh -c 'mongodump --uri="mongodb://username:password#localhost:27017/db_name?authSource=admin&readPreference=primary" --archive' > db.dump
I am doing it like this:
mongodump --host='{mongo_server}' --db='{mongo_db}' --collection='{collection_name}' --out={output_folder}
and it works just fine.
though as you can see i am doing collection by collection dump through python script on separate host, because i wasn't able to backup everything at once.
so the first step is to get all collections from container with installed mongo client:
mongo {mongo_server}/{mongo_db} --eval 'db.getCollectionNames()' --quiet
my script also uploads each collection dump to s3 bucket s3cmd, that is why you are seeing only part of it...
After weeks, i get a backup from files (not mongodump).
sudo tar czvf /home/backup.tar.gz /opt/rpg/mongo
And i did a repair and worked at my pc.
mongod --repair ./{{folder}}

run mongo in docker container

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.

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

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