2

I've followed the instructions in https://tsmx.net/docker-local-mongodb/ but I still get the following error:

**panic: unable to connect to MongoDB (local): no reachable servers **

I even tried the following but still get the same error:

_ = pflag.String("mongodb-addr", "127.0.0.1:27017", "MongoDB connection address")

My connection code is as follows:

dbAddr := d.cfg.GetString("mongodb-addr")
session, err := mgo.Dial(dbAddr)

And my docker run command is as follows:

docker run image_name

I'm using macOS Monterey. Any help would be greatly appreciated. Thanks.

Foobar
  • 843
  • 1
  • 10
  • 23

3 Answers3

0

If the application and the MongoDB are on the same docker network, then use the docker name to connect to the MongoDB container.

If the MongoDB is running in the server where the application is running in docker container, then use the IP of the server to communicate to the MongoDB. 127.0.0.1 from within the container will try to find the MongoDB within the same Docker as the application.

Abhishek S
  • 546
  • 3
  • 16
  • MongoDB is running on the host (i.e. localhost or 127.0.0.1 and outside of Docker) and the Go application is containerized (inside Docker). Using localhost or 127.0.0.1 in the connection address doesn't work. – Foobar Nov 02 '22 at 13:27
  • 2
    This link has answers on how to access a port on the host from a Docker container: https://stackoverflow.com/questions/24319662/ – Wild Zyzop Nov 02 '22 at 13:55
  • Then the above link has the solution. – Abhishek S Nov 02 '22 at 18:10
  • The link above only works for relational databases. I followed the advice in it for PostgreSQL and it worked. But it doesn't work for MongoDB. – Foobar Nov 02 '22 at 23:37
  • Can you try using the IP or hostname of the server where the MongoDB is running? – Abhishek S Nov 03 '22 at 08:35
  • I've tried with an explicit IP address, with localhost, with 127.0.0.1, and with host.docker.internal. It didn't make a difference. I had the same issue with PostgreSQL but it worked when I used host.docker.internal. – Foobar Nov 03 '22 at 14:03
  • Okay, so using **host.docker.internal:27017** works but now there is another issue that I notice in **mongo.log** which prevents the connection from persisting. Please see the 3 log lines below. These keep repeating themselves. – Foobar Nov 03 '22 at 23:31
0

if you run mongo like this :

mongo:
    image: mongo
    restart: always
    volumes: 
      - ./mongo-data:/data/db
    env_file: .env
    ports: 
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}

then you can connect from Go like this :

var cred options.Credential
cred.Username = MongoUsername
cred.Password = MongoPassword
clientOption := options.Client().ApplyURI(mongodb://mongodb:27017).SetAuth(cred)
  • I'm running MongoDB on localhost and it isn't containerized. This is a requirement so unfortunately the settings you have provided won't work. – Foobar Nov 02 '22 at 23:38
  • actually I'm working on localhost too... – mohammad mobasher Nov 03 '22 at 04:46
  • But your MongoDB is containerized, right? You're referencing a Mongo image in the docker compose file snippet above so I would imagine you are running it in a container. I'm running it outside of a container and trying to access it from a Go application which is running inside a container. Hence, our setups are different. – Foobar Nov 03 '22 at 13:53
  • I got it, yes, it's deference – mohammad mobasher Nov 04 '22 at 15:11
0

I was facing the same issue and this command did the trick for me, was mentioned here.

Docker provides a host network which lets containers share your host’s networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself.

docker run -d --network=host my-container:latest

hope it help someone.

eljapi
  • 146
  • 1
  • 4