Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-878

Driver doesn't seem to work when inside docker container

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.0.0
    • Component/s: API, Core API
    • Labels:
      None
    • Environment:

      For now a few days i'm trying to dockerize a new stack we are using at the company.  

      But i'm now stuck at a point where this driver running in a docker container doesn't want to connect to a mongodb server that is also running in a docker container.

      Here is the error i'm getting when i want to do any interaction with the mongodb server: 

      2019/03/14 14:27:30 server selection error: server selection timeout
      current topology: Type: Unknown
      Servers:
      Addr: localhost:27017, Type: Unknown, State: Connected, Avergage RTT: 0, Last error: dial tcp 127.0.0.1:27017: connect: connection refused
      

       This only happens when the app is inside a docker container.

       

      This is the code I've used to test this: 

      package mainpackage main
      import (
       "context"
       "fmt"
       "log"
       "go.mongodb.org/mongo-driver/bson"
       "go.mongodb.org/mongo-driver/mongo"
       "go.mongodb.org/mongo-driver/mongo/options"
      )
      
      func C() context.Context {
       return context.Background()
      }
      
      func main() {
       connectionURL := "mongodb://mongo?authMechanism=SCRAM-SHA-1"
       fmt.Println("Connecting to mongodb server:", connectionURL)
       client, err := mongo.Connect(C(), options.Client().ApplyURI(connectionURL))
       if err != nil {
        log.Panicln(err)
       }
       DB := client.Database("idk")
       _, err = DB.Collection("test").Find(C(), bson.M{})
       if err != nil {
        log.Panicln(err)
       }
      }
      

       

      How i run the database to test this: 

      docker run -p 27017:27017 --name mongo -it --rm mongo
      

       

      Then to run the app i build this docker container:

      FROM ubuntu
      WORKDIR /
      COPY ./testBinary /testBinary
      CMD /testBinary
      

      And to run it i do:

      docker run --link mongo:mongo -it --rm test-binary
      

      When running it hangs for around 30 seconds and after that it errors with the error specified above.

       

      I've tested this with multiple ways that work.

      • When running mongo-express in a docker container works:
        docker run --link mongo:mongo -it --rm -p 8081:8081 mongo-express
      • When running the mongoDB server in a container and the app directly on the system (not in a docker container)
      • when running the mongoDB server the normal way (no docker containers via systemctl) and the app directly on the system (also not in a docker container)

      Some notes:

      • This has nothing todo with --link because when running everything in a docker network it has the same issue.

            Assignee:
            Unassigned Unassigned
            Reporter:
            mjarkk Mark Kopenga
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: