Error 'import path does not begin with hostname' when building docker with local package

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I'm trying to build a docker with a local package but get the error 'import path does not begin with hostname'. If my understanding is correct, my Dockerfile should be just

FROM golang:onbuild EXPOSE 8080 

based on this article Deploying Go servers with Docker

I use this code git-go-websiteskeleton as the source for building the docker. the full error is here.

 import "git-go-websiteskeleton/app/common": import path does not begin with hostname package git-go-websiteskeleton/app/common: unrecognized import path "git-go-websiteskeleton/app/common" import "git-go-websiteskeleton/app/home": import path does not begin with hostname package git-go-websiteskeleton/app/home: unrecognized import path "git-go-websiteskeleton/app/home" import "git-go-websiteskeleton/app/user": import path does not begin with hostname package git-go-websiteskeleton/app/user: unrecognized import path "git-go-websiteskeleton/app/user" 

Thank you for a help.

回答1:

The application is built inside the docker container and you need to have your dependencies available when building.

golang:onbuild gives compact Dockerfiles for simple cases but it will not fetch your dependencies.

You can write your own Dockerfile with the steps needed to build your application. Depending on how your project looks you could use something like this:

FROM golang:1.6 ADD . /go/src/yourapplication RUN go get github.com/jadekler/git-go-websiteskeleton RUN go install yourapplication ENTRYPOINT /go/bin/yourapplication EXPOSE 8080 

This adds your source and your dependency into the container, builds your application, starts it, and exposes it under port 8080.



回答2:

Try :

FROM golang:latest RUN mkdir /go/src/app WORKDIR /go/src/app ADD ./HelloWorld.go ./ RUN go get RUN go build -o main . CMD ["/go/src/app/main"] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!