简体   繁体   中英

Go 1.11 unknown import path for own package in Docker build

I am migrating some code to work with Go 1.11 modules, and I am able to build it from the shell but not in Docker.

Relevant Dockerfile sections:

WORKDIR /goscout
COPY ["go.mod", "go.sum", "./"]
RUN GO111MODULE=on go get -u=patch
COPY *.go ./
RUN GO111MODULE=on go build -v -ldflags "-linkmode external -extldflags -static" -o GoScout -a .

When Docker is running the last command in the above excerpt, I get this error:

can't load package: package github.com/triplestrange/StrangeScout/goscout: unknown import path "github.com/triplestrange/StrangeScout/goscout": ambiguous import: found github.com/triplestrange/StrangeScout/goscout in multiple modules:
    github.com/triplestrange/StrangeScout/goscout (/goscout)
    github.com/triplestrange/StrangeScout v0.3.0 (/go/pkg/mod/github.com/triplestrange/!strange!scout@v0.3.0/goscout)

I don't get this in the shell, so I'm guessing I am not copying some files correctly. But before this command runs I have copied go.mod , go.sum , and *.go , so I don't know what could be missing.

Make sure that you initialized modules properly for your project

go mod init github.com/triplestrange/StrangeScout/goscout

so that the content of your go.mod is

module github.com/triplestrange/StrangeScout/goscout

And then you can use your current Dockerfile without any changes .

There is no need to set GO111MODULE=on since you're running go commands outside of the $GOPATH

➜ docker build -t goscout .
Sending build context to Docker daemon   47.1kB
Step 1/11 : FROM golang:latest AS builder
 ---> fb7a47d8605b
Step 2/11 : WORKDIR /goscout
 ---> Running in e9786fe5ab53
Removing intermediate container e9786fe5ab53
 ---> 6d101e346175
Step 3/11 : COPY ./ ./
 ---> 7081c0b47dc9
Step 4/11 : RUN go get -d -v ./...
 ---> Running in 3ce69359ae88
go: finding github.com/go-sql-driver/mysql v1.4.0
go: finding github.com/gorilla/mux v1.6.2
go: downloading github.com/gorilla/mux v1.6.2
go: downloading github.com/go-sql-driver/mysql v1.4.0
Removing intermediate container 3ce69359ae88

...


 ---> 3df0dbca80e5
Successfully built 3df0dbca80e5
Successfully tagged goscout:latest

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