Details
-
Bug
-
Resolution: Done
-
Major - P3
-
None
-
1.1.3
-
None
-
MacOS Docker Server: 19.03.5
Go: 1.13
Description
I was able to connect to Atlas with Single stage docker build. However, when I want to optimise the image, i go for multi stage docker build, it cannot connect to Atlas, without any change on code
Go code
url := "mongodb+srv://<USERNAME>:<PASSWORD>@cluster0-7odzu.mongodb.net/test?retryWrites=true&w=majority" |
|
|
client, err := mongo.NewClient(options.Client().ApplyURI(url))
|
log.Println("db client created") |
if err != nil { |
log.Fatal(err)
|
}
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
defer cancel()
|
err = client.Connect(ctx)
|
|
|
if err != nil { |
return nil, err |
}
|
log.Println("db client connected") |
|
|
err = client.Ping(ctx, readpref.Primary())
|
if err != nil { |
return nil, err |
}
|
log.Println("db client ping") |
Dockerfile
FROM golang:1.13 as build-env |
RUN mkdir /medicbot
|
WORKDIR /medicbot
|
COPY go.mod .
|
COPY go.sum .
|
|
|
RUN go mod download
|
COPY . .
|
|
|
RUN go build -o /go/bin/example
|
EXPOSE 5000 |
ENTRYPOINT ["/go/bin/example"] |
|
|
# Build the binary
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /go/bin/example |
FROM scratch
|
COPY --from=build-env /go/bin/example /go/bin/example
|
EXPOSE 5000 |
ENTRYPOINT ["/go/bin/example"] |
Current console output
2019/12/20 12:00:54 db.go:34: db client created |
2019/12/20 12:00:54 db.go:46: db client connected |
2019/12/20 12:01:14 main.go:28: context deadline exceeded |
Expected console output (when using single stage docker build)
2019/12/20 11:57:28 db client created |
2019/12/20 11:57:28 db client connected |
2019/12/20 11:57:28 db client ping |