Error when trying to use go mod download on docker

Viewed 3064

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"]
1 Answers

This looks like a networking issue.

When you are running outside of Docker, it is likely that you don't see these errors because the affected modules are already in your local module cache from a previous build, whereas your Dockerfile is starting from a clean cache and redownloading the module source every time.

Related