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