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

huangapple go评论77阅读模式
英文:

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

问题

我正在尝试使用本地包构建一个Docker容器,但是遇到了"import path does not begin with hostname"的错误。根据我理解,我的Dockerfile应该只包含以下内容:

FROM golang:onbuild
EXPOSE 8080

根据这篇文章Deploying Go servers with Docker

我使用这个代码git-go-websiteskeleton作为构建Docker容器的源代码。完整的错误信息如下:

<pre>
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"
</pre>

谢谢您的帮助。

英文:

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.

<pre>
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"
</pre>

Thank you for a help.

答案1

得分: 2

应用程序是在Docker容器内构建的,构建时需要有所需的依赖项。

golang:onbuild 提供了简单情况下紧凑的Dockerfile,但它不会获取您的依赖项。

您可以编写自己的Dockerfile,其中包含构建应用程序所需的步骤。根据您的项目情况,您可以使用类似以下内容的内容:

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

这将将您的源代码和依赖项添加到容器中,构建应用程序,启动它,并在8080端口下公开它。

英文:

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

得分: 1

尝试:

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"]
英文:

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 [&quot;/go/src/app/main&quot;]

huangapple
  • 本文由 发表于 2015年4月21日 22:15:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/29774786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定