Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"]
like image 914
Arsham Arya Avatar asked Sep 18 '25 03:09

Arsham Arya


2 Answers

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`
like image 62
Arsham Arya Avatar answered Sep 20 '25 17:09

Arsham Arya


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.

like image 22
bcmills Avatar answered Sep 20 '25 17:09

bcmills