skip to Main Content

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


  1. Chosen as BEST ANSWER

    Looks like I've fixed my own problem. I found another answer on here with a similar problem and replaced my Dockerfile with this:

    # syntax=docker/dockerfile:1
    
    FROM golang:1.22.2
    
    RUN mkdir /app
    ADD . /app
    WORKDIR /app
    
    COPY go.mod go.sum ./
    
    # Download all the dependencies
    RUN go mod download
    
    COPY . .
    
    
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
    

    This builds successfully. We also had a .dockerignore that was ignoring two subpackages that are actually used in the main.go, so I removed that to get rid of those errors.


  2. Try use this one docker code

    # syntax=docker/dockerfile:1
    FROM golang:1.22-alpine as builder
    
    ENV GO111MODULE=on 
        CGO_ENABLED=0 
        GOOS=linux 
        GOARCH=amd64
    
    WORKDIR /build
    
    RUN apk update && apk upgrade && 
        apk --no-cache add ca-certificates && 
        apk --no-cache add tzdata
    
    COPY go.mod .
    COPY go.sum .
    COPY .env .
    RUN go mod download
    
    COPY . .
    
    RUN go build -o test main.go
    
    WORKDIR /dist
    
    RUN cp /build/test .
    
    # Build a small image
    FROM gcr.io/distroless/static
    
    # copy files
    COPY --from=builder /dist/test /
    COPY .env .
    
    ENTRYPOINT [ "/test" ]
    
    EXPOSE 27017
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search