-1

I have an app written in go and gin, this file is divided as follows:

main.go 
    controller 
        controllers.go 
    database    
        database.go 
    middleware 
        middleware.go 
    models 
        models.go 
    routes 
        routes.go 
go.mod 
go.sum 
Dockerfile

and in each of these folders I have a package that calls another package, for example:

package controller

import (
    "log"
    "net/http"
    "os"

    gomail "gopkg.in/mail.v2"

    "github.com/dgrijalva/jwt-go"
    "github.com/gin-gonic/gin"
    models "github.com/guilherm5/login-user/models"
)

package routes

import (
    "github.com/gin-gonic/gin"
    controller "github.com/guilherm5/login-user/controller"
    middleware "github.com/guilherm5/login-user/middleware"
)

my go.mod is like this:

module github.com/guilherm5/login-user



go 1.20

require (
    github.com/dgrijalva/jwt-go v3.2.0+incompatible
    github.com/gin-gonic/gin v1.9.0
    github.com/golang-jwt/jwt v3.2.2+incompatible
    github.com/joho/godotenv v1.5.1
    github.com/lib/pq v1.10.9
    golang.org/x/crypto v0.9.0
    gopkg.in/mail.v2 v2.3.1
)

require (
    github.com/bytedance/sonic v1.8.0 // indirect
    github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
    github.com/gin-contrib/sse v0.1.0 // indirect
    github.com/go-playground/locales v0.14.1 // indirect
    github.com/go-playground/universal-translator v0.18.1 // indirect
    github.com/go-playground/validator/v10 v10.11.2 // indirect
    github.com/goccy/go-json v0.10.0 // indirect
    github.com/json-iterator/go v1.1.12 // indirect
    github.com/klauspost/cpuid/v2 v2.0.9 // indirect
    github.com/leodido/go-urn v1.2.1 // indirect
    github.com/mattn/go-isatty v0.0.17 // indirect
    github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
    github.com/modern-go/reflect2 v1.0.2 // indirect
    github.com/pelletier/go-toml/v2 v2.0.6 // indirect
    github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
    github.com/ugorji/go/codec v1.2.9 // indirect
    golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
    golang.org/x/net v0.10.0 // indirect
    golang.org/x/sys v0.8.0 // indirect
    golang.org/x/text v0.9.0 // indirect
    google.golang.org/protobuf v1.28.1 // indirect
    gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)

and so it goes following the flow of my application..

I want to make a docker image of this application (for learning purposes, I want to learn the basics of docker so I made this api for testing)

docker image:

FROM golang:1.20 as build 

WORKDIR /mail 

COPY go.sum ./
COPY go.mod ./
COPY main.go ./


RUN go mod download
RUN go build -o /appMail

FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=build /mail /mail

EXPOSE 4000/

USER nonroot:nonroot 

ENTRYPOINT ["/appMail"]

when trying to build the image I get the error:

=> ERROR [build 7/7] RUN go build -o /appMail             0.7s 
------
 > [build 7/7] RUN go build -o /appMail:
#13 0.680 main.go:5:2: no required module provides package github.com/guilherm5/login-user/routes; to add it:
#13 0.680       go get github.com/guilherm5/login-user/routes   
------
executor failed running [/bin/sh -c go build -o /appMail]: exit code: 1

does anyone know why? I really don't know what to do to solve it, how can I import my packages to the docker image?

-- EDIT

I updated my Dockerfile as per suggested to:

FROM golang:1.20 as build 

WORKDIR /mail 

COPY go.sum ./
COPY go.mod ./
COPY main.go ./

RUN go mod tidy
RUN go mod download
RUN go build -o /appMail

FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=build /mail /mail

EXPOSE 4000/

USER nonroot:nonroot 

ENTRYPOINT ["/appMail"]

ERROR:

github.com/guilherm5/login-user imports
#12 9.121       github.com/guilherm5/login-user/routes: cannot find module providing package github.com/guilherm5/login-user/routes: module github.com/guilherm5/login-user/routes: git ls-remote -q origin in /go/pkg/mod/cache/vcs/a7be81f5c5695c6941aa1b7f5a49aa0800b2d648c4d72609eb5feae6f51cf505: exit status 128:
#12 9.121       fatal: could not read Username for 'https://github.com': terminal prompts disabled
#12 9.121 Confirm the import path was entered correctly.        
#12 9.121 If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
------
executor failed running [/bin/sh -c go mod tidy]: exit code: 1 

1 Answers1

3

The issue is happening with COPY main.go ./ this.

How golang will looks the dependencies/packages ?

  • Module Cache : Check the package exists in the modcache $GOPATH/pkg/mod
  • Project Directory : If the package is not found in the modcache, then it will check the packages are exists relative to our projects module root. (github.com/guilherm5/login-user/models - check a directory named as models exists. )
  • Remote : If the packages are not found in the module cache or projects root, then fetch it from remote repository.

In your case you already have the package (directories) within your project module root directory, but only copied the main.go to the container system. Thus when go mod tidy runs, it couldn't be able to see the packages either in cache or root. Then it is trying to pull it from remote.

I have the code in remote repo, then why it is not pulling ? Because the repository is private.

Solution

Copy all the files and folder to the container system

copy . . 

and the issue will be resolved. You already have multistage build which will only have the necessary files in the final production build.

See

PRATHEESH PC
  • 1,461
  • 1
  • 3
  • 15