Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB docker container doesn't show databases

Tags:

docker

mongodb

I setup MongoDB docker container following this blog: https://medium.com/@kristaps.strals/docker-mongodb-net-core-a-good-time-e21f1acb4b7b

and when I run .net core app, save one todo, I manage to query it back, and it seems that everything works (todo collection is created, db is created, todo is saved). But when I access mongo db container using 'docker exec -it mongodb bash' and try to get dbs with 'show dbs' it retrieves empty list. Also, this docker compose boots up mongo express container which also doesn't list new db and collection. Do you know what can be an issue?

Cheers

like image 502
Bida Avatar asked Jun 10 '19 22:06

Bida


People also ask

How do I connect to a MongoDB database in a Docker container?

For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.

How do I view databases in MongoDB?

If you want to check your databases list, use the command show dbs. Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. In MongoDB default database is test.

Does Docker container include database?

Docker is great for running databases in a development environment! You can even use it for databases of small, non-critical projects which run on a single server. Just make sure to have regular backups (as you should in any case), and you'll be fine.

How do you display a list of databases in the MongoDB shell?

Listing all the databases in mongoDB console is using the command show dbs . For more information on mongo shell commands, refer the Mongo Shell Quick Reference.


1 Answers

It's likely that you have authentication enabled (username and password). This means that you have to first authenticate in the shell before you can list the databases.

Here's how that can be done (extra steps added for googlers):

    $ docker ps // list images

    IMAGE
    my-mongodb-container

$ docker exec -it my-mongodb-container bash

// -it is a flag for 'interactive terminal', bash is the shell we'll use

root@example:/# mongo 
// enter mongodb shell

> use admin 
// switched to db admin

> db.auth("username", "password");
// 1

> show dbs
admin  0.000GB
config 0.000GB
local  0.000GB
mydb   0.000GB

> use mydb
// switched to db mydb

You are now able to run mongodb shell commands inside your mongodb instance - so things like 'show collections' will work.

Hope that helps someone! This stuff is sometimes very opaque :)

like image 89
jacobedawson Avatar answered Oct 16 '22 17:10

jacobedawson