90

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?

Madhavi Jouhari
  • 1,969
  • 4
  • 23
  • 39

5 Answers5

192

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
Michael Mior
  • 27,152
  • 8
  • 85
  • 111
vladzam
  • 5,057
  • 6
  • 28
  • 35
  • 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 – Madhavi Jouhari Oct 05 '15 at 09:20
  • What version of docker do you currently have installed? The exec command has been introduced in version 1.3.0 – vladzam Oct 05 '15 at 09:22
  • 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). – booyaa Oct 05 '15 at 09:27
  • As booyaa mentioned, you should update docker on your host as you are currently using an old version that does not offer the latest available features – vladzam Oct 05 '15 at 09:29
  • I updated the docker version and it helped. Thanks a lot! – Madhavi Jouhari Oct 05 '15 at 09:45
18

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"
  • 2
    As a security precaution, use the `-p` flag, but don't enter your password as it will be present in your bash / shell history. – Jonathan Smith Nov 04 '19 at 12:52
2

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

mongo
Aman Rathod
  • 89
  • 1
  • 10
2

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"
Roman
  • 15,278
  • 11
  • 75
  • 80
1

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.

Madhavi Jouhari
  • 1,969
  • 4
  • 23
  • 39
  • 14
    How does that "start a mongodb shell in docker container"? – qqilihq Aug 31 '17 at 09:27
  • @qqilihq: The above mentioned commands started mongo container as a daemon in the backend. With this command I was able to connect to the running container, and execute the mongo queries in it. Although I get your point, this command doesnt "start" it, it connects to already running container. – Madhavi Jouhari Aug 31 '17 at 13:39
  • 7
    This command assumes you have mongo installed on your host computer. When you run mongodb inside a container, you won't have mongo executable on your host computer unless you install ot seperately. – Serdar Kalaycı Aug 20 '19 at 13:20
  • 1
    As others correctly say, this answer makes the strong assumption to already have mongo installed on the local machine. Nothing to do with Docker. – Andrea Apr 29 '21 at 08:43