mongodbdocker

How to start a mongodb shell in docker container?


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?


Solution

  • 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