Search code examples
dockereofgo-gormgo-modules

Error when trying to use go mod download on docker


I'm making an app using docker and Postgres and gorm (go ORM library) I have this Error (some kind of EOF?) while I want to build my docker image EOF error when go mod download on docker

My code is so simple and runs correctly without docker.

package main

import (
    "fmt"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

func main() {
    dsn := "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
    if err != nil {
        fmt.Println("Error connecting to postgres", err)
    }

    fmt.Printf("%+v\n", db)
}

Also, it's my Dockerfile:

FROM golang:1.16.4-alpine3.13 AS build
RUN mkdir /app
COPY src/go.mod /app
COPY src/go.sum /app
WORKDIR /app

RUN go mod download

COPY src /app
RUN go build -o main .

FROM alpine AS final
COPY --from=build /app /app
ENTRYPOINT ["/app/main"]

Solution

  • In my case, it was because of network issues, using a VPN or Proxy may help, I set this variable (GOPROXY) to my command and it worked:

    GOPROXY="https://goproxy.io" go mod download # or `go mod tidy`