Connecting Go with Mongodb using Docker

Asked

Viewed 647 times

5

I am creating a microservice in Go, using Mongodb as a database and using the library mgo.

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 Docker Compose file 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 code of the Docker-Compose:

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 makes the connection in Go:

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

The error launched by Go:

panic: no reachable servers

Running the ping in the microservice container, Mongodb returns confirming that there is a connection between the containers.

I have tried using the connection string from mongodb documentation and yet I was unsuccessful

  • I believe your problem is with Docker and not with the go itself. I am in doubt in those quotes on the value of the Environment key. That links in Docker Compose seems unnecessary to me too.

1 answer

3


As I am also "joking" with Docker recently, I decided to do some tests.

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"

Only run:

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 isn’t the go itself, but rather those extra quotes in the MONGO_URL of the Docker-Compose.yaml.

  • It was exactly this, thank you very much, I did not know that in the environment variables could not have quotation marks when it refers to another service, I thought that as it would put in the hosts, only the name would be enough.

  • 1

    To be honest, I didn’t know either! So I decided to test here and learn along with you. No need for quotes, even when there are spaces in the environment variable. : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.