127

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?

7 Answers 7

254

You can run the interactive mongo shell by running the following command:

docker run -it -p 28000:27017 --name mongoContainer mongo:latest mongosh

Otherwise, if your container is already running, you can use the exec command:

docker exec -it mongoContainer mongosh
Sign up to request clarification or add additional context in comments.

8 Comments

Error: Command not found: exec Error: Command not found: -it Usage: docker [OPTIONS] COMMAND [arg...] -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to use
you need to upgrade, we're up to 1.8 and there's been a lot of changes to the plumbing of docker. that or use nsentry (not sure if it support your version of docker).
mongo no longer works, use mongosh instead.
As @Herii said docker exec -it mongoContainer mongo results in bash: mongo: command not found. Please update your answer to use mongosh
in Windows 11 and Docker desktop version 4.17, this command worked for me: "docker exec -it "YourContainerName" mongo"
|
28

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"

1 Comment

As a security precaution, use the -p flag, but don't enter your password as it will be present in your bash / shell history.
22

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

Comments

13

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"

Comments

6

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

Comments

6
docker exec -it mongoContainer mongosh

i tried mongo, mongod, and mongodb but failed. Then i changed it to mongosh and it works!

Comments

0

Network : External Network

docker network create local_central_db_network

create file docker-compose.yml

version: '3.3'
services:
  mongo_db:
    image: mongo
    container_name: local-central-mongo-db-container
    ports:
      - "27017:27017"
    restart: always
    volumes:
      - ./mongo_db:/data/db
    networks:
      - local_central_db_network
networks:
  local_central_db_network:
    external: true

Commands

Docker

sudo docker-compose down
sudo docker-compose build && docker-compose up -d

mongosh
show collections
show dbs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.