Connecting Go with MongoDB using Docker

5

I'm creating a microservice in Go, using MongoDB as the database and using the mgo library.

Using MongoDB in a docker container locally and running the application on my machine, everything works normally, but when I tried to run using a file from docker compose I could not make the connection between Go and MongoDB

The Dockerfile used to generate the image:

FROM golang:1.9.2 as builder
WORKDIR /app
RUN go get -u gopkg.in/mgo.v2
RUN go get -u github.com/gin-gonic/gin
COPY .  .
RUN CGO_ENABLED=0 GOOS=linux go build -o microservice .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/microservice .
EXPOSE 8080
CMD ["./microservice"]

The docker-compose code:

version: '3'
services:
  api-user:
    image: "api-user"
    links:
     - "mongodb"
    environment:
     - MONGO_URL="mongodb"
    depends_on:
      - mongodb

  mongodb:
    image: "mongo"

This is the code that connects to Go:

session, err := mgo.Dial(os.Getenv("MONGO_URL"))
if err != nil {
    panic(err)
}

The error posted by Go:

panic: no reachable servers

By pinging the microservice container, MongoDB returns confirming that there is a connection between the containers.

I've tried using the connection string from the MongoDB documentation and even then I did not have success

    
asked by anonymous 13.12.2017 / 21:42

1 answer

3

As I've been "playing" with docker recently, I've decided to do some testing.

main.go

package main

import (
    "fmt"
    "os"

    mgo "gopkg.in/mgo.v2"
)

func main() {
    url := os.Getenv("MONGO_URL")

    fmt.Printf("[%v]\n", url)
    session, err := mgo.Dial(url)
    if err != nil {
        panic(err)
    }

    fmt.Println("ping", session.Ping())
}

Dockerfile

FROM scratch
COPY myapp /
CMD ["/myapp"]

docker-compose.yaml

version: "3"

services:
  myapp:
    build: .
    image: custom-myapp
    environment: 
      - MONGO_URL=mongodb
    depends_on:
      - mongodb

  mongodb:
    image: "mongo"

Run only:

  

docker-compose up --build --force-recreate

Output:

Building myapp
Step 1/3 : FROM scratch
 --->
Step 2/3 : COPY docker /
 ---> Using cache
 ---> 9b265efb7057
Step 3/3 : CMD /docker
 ---> Using cache
 ---> 8e8ac4e3cb71
Successfully built 8e8ac4e3cb71
Successfully tagged custom-myapp:latest
Recreating docker_mongodb_1 ...
Recreating docker_mongodb_1 ... done
Recreating docker_myapp_1 ...
Recreating docker_myapp_1 ... done
Attaching to docker_mongodb_1, docker_myapp_1
mongodb_1  | 2017-12-14T01:31:43.888+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=c4db7d1bfabc
mongodb_1  | 2017-12-14T01:31:43.888+0000 I CONTROL  [initandlisten] db version v3.6.0
mongodb_1  | 2017-12-14T01:31:43.888+0000 I CONTROL  [initandlisten] git version: a57d8e71e6998a2d0afde7edc11bd23e5661c915
myapp_1    | [mongodb]
mongodb_1  | 2017-12-14T01:31:43.888+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
myapp_1    | ping <nil>
mongodb_1  | 2017-12-14T01:31:43.888+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongodb_1  | 2017-12-14T01:31:43.888+0000 I CONTROL  [initandlisten] modules: none
...
mongodb_1  | 2017-12-14T01:31:44.304+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
mongodb_1  | 2017-12-14T01:31:45.476+0000 I NETWORK  [listener] connection accepted from 172.20.0.3:58192 #1 (1 connection now open)
mongodb_1  | 2017-12-14T01:31:45.542+0000 I NETWORK  [conn1] end connection 172.20.0.3:58192 (0 connections now open)
docker_myapp_1 exited with code 0
Gracefully stopping... (press Ctrl+C again to force)
Stopping docker_mongodb_1 ... done

As you can see there, as soon as the server became available the connection was made.

The problem really is not go itself, but rather those extra quotation marks in MONGO_URL of docker-compose.yaml.

    
14.12.2017 / 02:35