Search code examples
mongodbmern

the mongodb docker container is not connecting


Initial scenario

So I created a docker container for mongo db named blog_db

command docker run --name blogs_db -d mongo

Commands tried

docker ps

Output

CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS          PORTS                                           NAMES
85dc17990758   mongo:7   "docker-entrypoint.s…"   35 hours ago   Up 48 minutes   0.0.0.0:27017->27017/tcp, 27017/tcp   blogs_db

I didn't knew what to do

I searched a lot still no results.
I tried changing localhost to 127.0.0.1 and 0.0.0.0 still no improvement.


Solution

  • What ended up working

    I got it late after checking the logs and configs.
    A bunch of yt videos and articles.

    I tried connecting to other container of mongodb

    docker ps
    CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS         PORTS                                           NAMES
    004611add127   mongo:7   "docker-entrypoint.s…"   2 weeks ago   Up 5 seconds   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   passport-auth
    
    

    It was working and then I noticed :::27017->27017/tcp

    I got it -p was what I missed.
    I thought since 27017 is the default port I do not need to write it.

    But I was so wrong, it has to be there.
    I spent a day trying to figure it out,
    deleted the current instance
    and created a fresh one with the -p and it worked.

    Take Backup before doing so

    docker cp blog_db:/data/db /path/to/backup/directory
    

    then

    docker stop blog_db
    docker rm blog_db
    
    docker run --name blog_db -p 27017:27017 -v blog_db_data:/data/db -d mongo
    

    Learning

    Type your commands completely else you will have to debug it badly.
    At times even debugging won't work.