I am trying to create a docker image for this sample project structure of it as follows
- two modules - common and go-modules
Following is my Docker file
FROM golang as builder
ENV GO111MODULE=on
WORKDIR /go-modules-docker
COPY . .
COPY ./go-modules/go.mod .
COPY ./go-modules/go.sum .
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go
FROM alpine:3.8
WORKDIR /root/
COPY --from=builder /go-modules/app .
CMD ["./app"]
go.mod file is as follows
module go-modules
go 1.15
replace common => /go-modules-docker/common
require (
common v0.0.0-00010101000000-000000000000
github.com/julienschmidt/httprouter v1.3.0
)
Main.go is as follows
package main
import (
"fmt"
"log"
"net/http"
"go-modules/greet" // go-modules is our project namespace
"common/deps"
"github.com/julienschmidt/httprouter"
)
func main() {
r := httprouter.New()
r.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "hello world, v%v %v", greet.Version, deps.Version2)
})
log.Println("listening to port *:8080. press ctrl + c to cancel.")
log.Fatal(http.ListenAndServe(":8080", r))
}
When running this command from parent directory - go-modules-docker
docker build -t go-mod-docker -f go-modules/Dockerfile .
I am getting the following error
[+] Building 2.2s (13/15)
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.8 0.8s
=> [internal] load metadata for docker.io/library/golang:latest 0.8s
=> [internal] load build context 0.0s
=> => transferring context: 618B 0.0s
=> [builder 1/7] FROM docker.io/library/golang@sha256:cf46c759511d0376c706a923f2800762948d4ea1a9290360720d5124a730ed63 0.0s
=> [stage-1 1/3] FROM docker.io/library/alpine:3.8@sha256:2bb501e6173d9d006e56de5bce2720eb06396803300fe1687b58a7ff32bf4c14 0.0s
=> CACHED [builder 2/7] WORKDIR /go-modules-docker 0.0s
=> [builder 3/7] COPY . . 0.0s
=> [builder 4/7] COPY ./go-modules/go.mod . 0.0s
=> [builder 5/7] COPY ./go-modules/go.sum . 0.0s
=> [builder 6/7] RUN go mod download 0.8s
=> ERROR [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go 0.5s
------
> [builder 7/7] RUN CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go:
#14 0.434 go-modules/main.go:8:2: package go-modules/greet is not in GOROOT (/usr/local/go/src/go-modules/greet)
------
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -o app ./go-modules/main.go]: runc did not terminate sucessfully
Can anyone suggest what can be done and how to solve this problem?