title: "Server Selection Timeout Error" MongoDB Go Driver with Docker
body: <p>I'm working on a very basic (I thought) starter program in Go using MongoDB and Docker. Trying to get a handle on these before we start using them at work.
  I've got my MongoDB running in a docker container, just using my local host, using the official Docker image. This is running fine, I can connect to it through MongoDB Compass and modify the DB. </p>
  
  <p>My next task was to build a separate Docker container that is able to read and write to the DB. I'm using MongoDB-Go-Driver (<a href="https://godoc.org/github.com/mongodb/mongo-go-driver/mongo" rel="noreferrer">https://godoc.org/github.com/mongodb/mongo-go-driver/mongo</a>) for this as mgo is no longer kept up. </p>
  
  <p>This is my code, I'm just following the numerous tutorials online to make a simple connection and then ping the DB to ensure connectivity. </p>
  
  <pre><code>client, err := mongo.Connect("mongodb://localhost:27017")
  
  if err != nil {
      log.Fatal("error ", err)
  }
  
  // Check the connection
  err = client.Ping(context.TODO(), nil)
  
  if err != nil {
      log.Fatal("error2 ", err)
  }
  
  fmt.Println("Connected to MongoDB!")
  </code></pre>
  
  <p>It always fails on doing any operation on the DB (Find, FindOne, Ping, etc.) with <code>error2 server selection timeout</code></p>
  
  <p>This is my docker-compose file I'm running. </p>
  
  <pre><code>version: "3"
  
  services:
    datastore:
      image: mongo
      ports: 
        - "27017:27017"
      networks: 
        - maccaptionNet
      volumes: 
        - .:/go/src/maccaption_microservice/dbdata
    jobservice:
      image: jobservicemaccaption:1.0
      networks:
        - maccaptionNet
      depends_on:
        - "datastore"
  
  
  networks: 
    maccaptionNet:
      driver: bridge
  </code></pre>
  
  <p>I'm brand new to MongoDB and after hours of research haven't made any progress on this.
  I've read through <a href="https://docs.mongodb.com/manual/core/read-preference-mechanics/" rel="noreferrer">https://docs.mongodb.com/manual/core/read-preference-mechanics/</a>
  <a href="https://docs.mongodb.com/manual/replication/" rel="noreferrer">https://docs.mongodb.com/manual/replication/</a></p>
  
  <p>Can anyone point me in the right direction for this? I haven't been able to find a lot on this specific issue. </p>
  
  <p>Thanks!</p>