0

I am trying to create a docker image for this sample project structure of it as follows

  • two modules - common and go-modules

enter image description here

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?

Anshul Jhawar
  • 137
  • 1
  • 9
  • 1
    Does it print out the image file you attached, or some text-format errors instead? Can you replace the screen shot with the actual text of the error message? – David Maze Dec 01 '20 at 17:45
  • see https://stackoverflow.com/questions/61845013/package-xxx-is-not-in-goroot-when-building-golang-project – ozs Dec 01 '20 at 18:01
  • 1
    Please don't post images of text. You are running `go build` with a single filename, which isn't going to use modules. Run `go build` with a package from within the module. – JimB Dec 01 '20 at 18:09
  • @DavidMaze Have edited the question to replace error image with error texts. – Anshul Jhawar Dec 02 '20 at 02:17
  • @JimB as you mentioned, I think the issue is that build command is run from outside of the module. Since it is a multiple module and common module is being used in main module .. I have to run build command from outside of module (to copy common folder in Dockerfile). – Anshul Jhawar Dec 02 '20 at 02:18
  • You cannot build a module from outside the module. I don’t understand what you’re trying to do. – JimB Dec 02 '20 at 02:32
  • @JimB Agree ... I was referring to docker build (and not go build) – Anshul Jhawar Dec 02 '20 at 16:52

1 Answers1

1

Issue was solved by updating the WORKDIR again the Dockerfile to the go-modules (Basically doing a CD - change directory) and building the whole module

Changing docker file to this fixed the issue

FROM golang as builder

ENV GO111MODULE=on

WORKDIR /go-modules-docker

COPY . .

WORKDIR /go-modules-docker/go-modules

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux go build -o /go-modules-docker/app

FROM alpine:3.8

WORKDIR /root/

COPY --from=builder /go-modules-docker/app .

CMD ["./app"]
Anshul Jhawar
  • 137
  • 1
  • 9