查找golang dockerfile的路径时出错

im trying to build docker image with my golang project

I use the following

#build stage
FROM golang:alpine as builder
WORKDIR /go/src/app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o main .
RUN apk add --no-cache git


#final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /go/bin/app /app
ENTRYPOINT ./app
LABEL Name=fzr-dbc  Version=0.0.1
EXPOSE 3000

This build is failing in my main.go file which is looks like following

package main

import (
    "fzr-dbc/cmd/tsr”
)

func main() {
    tsr.Execute()
}

when I run the command docker build -t fzr . The error is:

main.go:4:2: cannot find package "fzr-dbc/cmd/tsr” in any of:
        /go/src/app/vendor/fzr-dbc/cmd/tsr (vendor tree)
        /usr/local/go/src/fzr-dbc/cmd/tsr (from $GOROOT)
        /go/src/fzr-dbc/cmd/tsrs (from $GOPATH)

The error since it’s not finding my project path , what could be missing here ?

The docker file is in my root project fzr and I run the docker build from there

You are running go build in your Dockerfile:

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o main .

This requires you to install all your dependencies for building it.

To fix this, either:

  1. Build the binary on your host and COPY the binary directly into the docker; or,
  2. Install dependencies inside your docker as part of RUN processes:
    • For Go before 1.11, you need to move your project source code into proper directory in GOPATH. Then, on that folder, run go get -u ./...
    • For Go 1.11+, you need to use Go 1.11 with go.mod and go.sum properly defined. Then you can use the command go mod download to download all the dependencies to the Go package cache inside your docker image.