I’m working to containerize a Golang app using Docker, and I’m running into an issue with getting the executable to build from my Dockerfile.
Here is the Dockerfile:
# syntax=docker/dockerfile:1
FROM golang:1.22.2
WORKDIR /app
# Download Go modules
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /sac
EXPOSE 8080
# Run
CMD ["/sac"]
Pretty much ripped straight from the Docker documentation on building Go images. When I go to build this (docker build --tag sac-server .
), I run into a bunch off errors that all look like this:
main.go:10:2: no required module provides package github.com/GenerateNU/sac/backend/config; to add it:
go get github.com/GenerateNU/sac/backend/config
I get a bunch of these for various modules in our package. The full project’s name (defined in the (go.mod
) is module github.com/GenerateNU/sac/backend
, and in this example /config is a sub-package. Our directory looks like this:
backend
--->auth
--->config
--->constants
--->database
---><a bunch of other modules>
--->go.mod
--->go.sum
--->main.go
--->Dockerfile
And all the errors we’re getting about ‘no required modules’ are submodules/packages referenced the main.go.
How do I go about fixing this? I’ve tried replacing the go mod download
with go get -d -v .
and go mod tidy
, but I get the same errors. Thank you! In case I didn’t provide enough info about our codebase, you can check out our repo link here: https://github.com/generateNU/sac
2
Answers
Looks like I've fixed my own problem. I found another answer on here with a similar problem and replaced my Dockerfile with this:
This builds successfully. We also had a
.dockerignore
that was ignoring two subpackages that are actually used in themain.go
, so I removed that to get rid of those errors.Try use this one docker code