When deploy to server issue with "no required module provides package"

I got issue when i build docker in my server but i don’t know why i haven’t localy, here is the stack trace:
"[6/6] RUN go build -o /main:
0.505 handlers/handlers.go:5:2: no required module provides package githubcom/rousseau-romain/round-timing/shared/components; to add it:
0.505 go get githubcom/rousseau-romain/round-timing/shared/components
0.505 handlers/auth.go:9:2: no required module provides package githubcom/rousseau-romain/round-timing/views/page; to add it:
0.505 go get githubcom/rousseau-romain/round-timing/views/page

Dockerfile:11
9 | COPY . .
10 |
11 | >>> RUN go build -o /main
12 |
13 | EXPOSE 2468

ERROR: failed to solve: process “/bin/sh -c go build -o /main” did not complete successfully: exit code: 1"

My repository is public i don’t think it’s right issue, but maybe i’m wrong.

https://github.com/rousseau-romain/round-timing

I had to replace “github.com/” to “githubcom/” with the link limitation.

Hope somebody know how to help me

  1. Check whether the go.mod file is correct
  2. Execute go mod vendor on the external host and copy the external library offline into the container.

The go mod file looks good and updated.
I tried to add go mod vendor in my dockerfile before my go mod download but i got issue to find external library.
“[7/7] RUN go build -o /main
#11 0.378 main.go:11:2: cannot find module providing package github.com/gorilla/mux: import lookup disabled by -mod=vendor
#11 0.378 (Go version in go.mod is at least 1.14 and vendor directory exists.)
#11 0.378 config/config.go:8:2: cannot find module providing package github.com/joho/godotenv: import lookup disabled by -mod=vendor”

In order to copy the external library offline into the container i need to have my vendor directory inside my container, localy when i build my container localy i got my directory with external library.

The package github.com/rousseau-romain/ is part of my local project, it’s also considered as external library ?

Please provide the project directory structure, including the dockerfile if possible.

There is the github link https://github.com/rousseau-romain/round-timing

The structure is like this:

├── config
├── database
│   └── migration
├── handlers
├── helper
├── model
├── public
│   ├── img
│   │   ├── class
│   │   └── spell
│   │       └── images
│   └── script
├── service
│   └── auth
├── shared
│   └── components
├── tmp
├── vendor
│   ├── github.com
│   │   ├── a-h
│   │   │   └── templ
│   │   │       └── safehtml
│   │   ├── go-sql-driver
│   │   │   └── mysql
│   │   ├── golang
│   │   │   └── protobuf
│   │   │       └── proto
│   │   ├── gorilla
│   │   │   ├── mux
│   │   │   ├── securecookie
│   │   │   └── sessions
│   │   ├── huandu
│   │   │   ├── go-sqlbuilder
│   │   │   └── xstrings
│   │   ├── joho
│   │   │   └── godotenv
│   │   └── markbates
│   │       └── goth
│   │           ├── gothic
│   │           └── providers
│   │               ├── discord
│   │               └── github
│   ├── golang.org
│   │   └── x
│   │       └── oauth2
│   │           └── internal
│   └── google.golang.org
│       ├── appengine
│       │   ├── internal
│       │   │   ├── base
│       │   │   ├── datastore
│       │   │   ├── log
│       │   │   ├── remote_api
│       │   │   └── urlfetch
│       │   └── urlfetch
│       └── protobuf
│           ├── encoding
│           │   ├── prototext
│           │   └── protowire
│           ├── internal
│           │   ├── descfmt
│           │   ├── descopts
│           │   ├── detrand
│           │   ├── encoding
│           │   │   ├── defval
│           │   │   ├── messageset
│           │   │   ├── tag
│           │   │   └── text
│           │   ├── errors
│           │   ├── filedesc
│           │   ├── filetype
│           │   ├── flags
│           │   ├── genid
│           │   ├── impl
│           │   ├── order
│           │   ├── pragma
│           │   ├── set
│           │   ├── strs
│           │   └── version
│           ├── proto
│           ├── reflect
│           │   ├── protodesc
│           │   ├── protoreflect
│           │   └── protoregistry
│           ├── runtime
│           │   ├── protoiface
│           │   └── protoimpl
│           └── types
│               └── descriptorpb
└── views
    └── page

The dockerfile is:

FROM golang:1.22-alpine

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN go build -o /main

EXPOSE 2468

CMD ["/main"]

Where is your main package? You should try (I’m assuming you go.mod in . dir):

rm -rf ./vendor
go mod tidy
go mod vendor
go build -o main . //if . is main package and can try build ...
FROM golang:1.22-alpine

WORKDIR /app

COPY . .

RUN go mod tidy // try again

RUN go build -o /main . //if . is main package -> /app

EXPOSE 2468

CMD ["/main"]

Yes my main package is ., i tried but git was not installed in alpine image so i used golang:1.22 instead and no git issue.

But now i got error with go mod tidy only in server.
I need to create version tag in my github repo in order to build in server ?

> [4/5] RUN go mod tidy:
1.292 go: downloading github.com/google/go-cmp v0.6.0
1.344 go: downloading google.golang.org/appengine v1.6.8
1.444 go: downloading github.com/golang/protobuf v1.5.3
1.750 go: downloading google.golang.org/protobuf v1.32.0
2.088 go: finding module for package github.com/rousseau-romain/round-timing/views/page
2.089 go: finding module for package github.com/rousseau-romain/round-timing/shared/components
5.754 go: github.com/rousseau-romain/round-timing/handlers imports
5.754 	github.com/rousseau-romain/round-timing/shared/components: no matching versions for query "latest"
5.754 go: github.com/rousseau-romain/round-timing/handlers imports
5.754 	github.com/rousseau-romain/round-timing/views/page: no matching versions for query "latest"

You have a problem with your code, you have a problem with import.
Have you really tried a local build? Such code knows at a glance that it cannot be compiled.

When i compile my code localy i got no issue, weird.
Maybe my configuration/installation is not good locally, i will try to remove go install and reinstall.

➜  round-timing git:(master) ✗ docker build -t round-timing . 
[+] Building 9.4s (10/10) FINISHED                                                                                                                                                                          
 => [internal] load build definition from Dockerfile                                                                                                                                                   0.0s
 => => transferring dockerfile: 73B                                                                                                                                                                    0.0s
 => [internal] load .dockerignore                                                                                                                                                                      0.0s
 => => transferring context: 2B                                                                                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/golang:1.22                                                                                                                                         0.0s
 => [internal] load build context                                                                                                                                                                      0.1s
 => => transferring context: 103.52kB                                                                                                                                                                  0.1s
 => [1/5] FROM docker.io/library/golang:1.22                                                                                                                                                           0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                                                                                          0.0s
 => [3/5] COPY . .                                                                                                                                                                                     0.4s
 => [4/5] RUN go mod tidy                                                                                                                                                                              1.2s
 => [5/5] RUN go build -o /main .                                                                                                                                                                      7.3s 
 => exporting to image                                                                                                                                                                                 0.4s 
 => => exporting layers                                                                                                                                                                                0.4s 
 => => writing image sha256:b5de9bc0452740ecd2c9800f2fc1371dda31c1388339c66936bc90c63b64417d                                                                                                           0.0s 
 => => naming to docker.io/library/round-timing   

The import for my local is not the right way to do it ?

github.com/rousseau-romain/round-timing/views/page
It’s not a valid go package path because there isn’t a go file in it, and I don’t know how you run it, but there’s something wrong with the code itself.

Ok thanks i take a look !

If you have the behavior of generating code, then you should commit the generated code to Git instead of leaving a prototype.

1 Like

I found the issue, i’m so dumb lol.
I use templ to generate gofile from.templ file and i didn’t want to commit the generated file.
So obviously if i don’t generate the go file inside docker it wont work, i have added in my Dockerfile:

RUN go install github.com/a-h/templ/cmd/templ@v0.2.793
RUN templ generate

Thx for your time and your help @peakedshout !

This is not a good practice. With build, if you can’t provide a make build flow, then you should ensure code integrity.
The grpc protobuf, for example, generates code, but typically git commits the generated code instead of letting the builder generate it. (Differences in generation tools can lead to differences in generated code)