How to start a mongodb shell in docker container?

后端 未结 3 1506
故里飘歌
故里飘歌 2020-12-22 19:12

To start the container, I am typing the following command:

sudo docker run -i -t -p 28000:27017 mongo:latest /usr/bin/mongod --smallfiles

B

相关标签:
3条回答
  • 2020-12-22 19:36

    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"
    
    0 讨论(0)
  • 2020-12-22 19:41

    This is an alternate way: Open a new terminal

    mongo 127.0.0.1:28000
    

    Your mongo shell starts in this terminal now.

    0 讨论(0)
  • 2020-12-22 19:50

    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
    
    0 讨论(0)
提交回复
热议问题