Docker golang package import error : import path d

2019-07-13 21:27发布

问题:

I am trying to test docker and go project. Here is my dockerfile

FROM golang

ARG app_env
ENV APP_ENV $app_env

COPY ./ /go/src/github.com/user/myProject/app
WORKDIR /go/src/github.com/user/myProject/app



RUN go get ./
RUN go build

CMD if [ ${APP_ENV} = production ]; \
then \
app; \
else \
go get github.com/pilu/fresh && \
fresh; \
fi  
EXPOSE 8080

It runs fine. Then i added a package "testpack" to my go program.

package main

import(
"fmt"
"time"
"testpack"
)

var now = time.Now()
var election = time.Date(2016, time.November, 8, 0, 0, 0, 0, time.UTC)
func main() {
 //get duration between election date and now
tillElection := election.Sub(now)
//get duration in nanoseconds
toNanoseconds := tillElection.Nanoseconds()
//calculate hours from toNanoseconds
hours := toNanoseconds/3600000000000
remainder := toNanoseconds%3600000000000
//derive minutes from remainder of hours
minutes := remainder/60000000000
remainder = remainder%60000000000
//derive seconds from remainder of minutes
seconds := remainder/1000000000 
//calculate days and get hours left from remainder
days := hours/24
hoursLeft := hours%24

fmt.Printf("\nHow long until the 2016 U.S. Presidential election?\n\n%v Days %v Hours %v Minutes %v Seconds\n\n", days, hoursLeft, minutes, seconds)

}

Now i ran=> docker build ./

I am getting an error

package testpack: unrecognized import path "testpack" (import path does not begin with hostname)

I tried this Error 'import path does not begin with hostname' when building docker with local package but couldn't resolve

Any help is appreciated.

回答1:

It is obviously trying to load it from the Internet because it isn't finding "testpack" in your GOPATH.

You didn't show us your GOPATH setting or where you copied "testpack" to, so other than saying "It's missing" that's all I can tell you.

Read https://golang.org/cmd/go/#hdr-Relative_import_paths

Try either

  • import "./testpack"
  • Set GOPATH to "/go" in your Dockerfile
    import "github.com/user/myProject/app/testpack"


回答2:

It sounds an awful lot like you're having a problem building your app inside the docker build process. This is likely a dependency issue (you have a dependency installed on your local $GOPATH that is not installed inside the image's go environment). You could install the dependency before the build command in the Dockerfile, but I would pretty seriously consider building the app outside of the Dockerfile, and copying the executable into the image on build.

One of the biggest advantages of Golang is the statically compiled executables. Once it is compiled, you should be able to run it in any equivalent architecture. By default, go will try to compile a static executable but if you would like to enforce it (and you are not doing anything fancy with CGO) you can build with the CGO_ENABLED env var set to 0 like so: CGO_ENABLED=0 go build -o <output.name> <file.to.build>

At this point, your Dockerfile becomes much simpler (and SMALLER, check the image size of the resulting images), something like:

FROM scratch

#copy executable into container
ADD <output.name> <output.name>

#set entrypoint
ENTRYPOINT [./<output.name>]

#set exposed port
EXPOSE 8080

This should solve your dependency issue, and make your runtime container much smaller (probably < 20MB), which will decrease build times and increase deployment speed.



标签: docker go