简体   繁体   中英

Building Go project from Dockerfile says package not in GOROOT

This is the Dockerfile I have:

FROM golang:1.13 AS go-build-env

FROM utilities:debug

# Build go binary
RUN apk add gcc go
ENV GO111MODULE=on \
    CGO_ENABLED=1 \
    GOOS=linux \
    GOARCH=amd64
COPY myApp/* myApp/
RUN cd myApp && go build && mv ./myApp ../

CMD bash

I am getting the following error when it is doing go build :

 > [stage-1 5/8] RUN cd myApp && go build && mv ./myApp ../:                                              
#12 0.751 found packages main (main.go) and codec (zwrapper.go) in /myApp
#12 0.751 main.go:17:2: package codec is not in GOROOT (/usr/lib/go/src/codec)

My GoApp structure is:

myApp
├── codec
│   └── zwrapper.go
├── go.mod
├── go.sum
├── main.go
└── vendor
    ├── codec
    │   └── zwrapper.go
    ├── github.com
    ...

When I run go build from myApp directory manually it works, but when building the Dockerfile, it keeps complaining. Do I have to setup a GOPATH and GOROOT in the Dockerfile? Any ideas for how to get around this?

Go has a system variable set location for the build location (which is strict). This is either your home directory + go/src/ or the GOPATH.

In your case you have to set your GOPATH:

ENV GOPATH /myApp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM