在使用Docker容器构建应用程序时遇到了“无法找到包”的问题。

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

getting "cannot find package" trying to build my application in a docker container

问题

这是我的Dockerfile。

FROM ubuntu
MAINTAINER me <my@email.com>
RUN apt-get update && apt-get install -y \
build-essential \
curl \
git \
make
# 获取并编译go
RUN curl -s https://go.googlecode.com/files/go1.2.1.src.tar.gz | tar -v -C /usr/local -xz
RUN cd /usr/local/go/src && ./make.bash --no-clean 2>&1
ENV PATH /usr/local/go/bin:/go/bin:$PATH
ENV GOPATH /go
RUN go get github.com/gorilla/feeds
WORKDIR /go 
CMD go version && go install feed && feed

它可以成功构建:

sudo docker build -t ubuntu-go .

但是当我运行它时,我遇到了一个包错误:

sudo docker run -v /home/rbucker/go:/go --name go ubuntu-go

错误信息如下:

src/feed/feed.go:7:2: 找不到包"github.com/gorilla/feeds",在以下任何位置都找不到:
/usr/local/go/src/pkg/github.com/gorilla/feeds (来自$GOROOT)
/go/src/github.com/gorilla/feeds (来自$GOPATH)

这很奇怪,因为"go install"没有安装依赖项,而之前的"go get github.com/gorilla/feeds"完成时没有出现错误。所以我可能有一个路径或环境问题,但是所有的示例看起来都和这个一样。

附注:我的代码位于/go/src/feed (feed.go)

package main

import (
    "net/http"
    "time"

    "github.com/gorilla/feeds"
)

. . .

更新:当我手动执行"go get"然后启动"run"时,似乎可以工作。所以看起来"go get"将我的文件存储在虚拟机中而不是我的主机卷中。

sudo docker run -v /home/rbucker/go:/go --name go ubuntu-go /bin/bash

然后

sudo docker run -v /home/rbucker/go:/go --name go ubuntu-go

(文件位于我的~/go/src/githum.com和~/go/pkg文件夹中。)

更新:我意识到在构建步骤中,/go卷还没有附加到Docker镜像上。所以它实际上被分配为nil。但是在运行时,"go get install"应该已经检索到它的依赖项。

最后:这个方法可以工作,但显然不是首选方法:

CMD go get github.com/gorilla/feeds && go version && go install feed && feed

请注意,我在CMD中执行了"go get"而不是在RUN中执行。

英文:

Here is my Dockerfile.

FROM ubuntu
MAINTAINER me &lt;my@email.com&gt;
RUN apt-get update &amp;&amp; apt-get install -y \
build-essential \
curl \
git \
make
# Get and compile go
RUN curl -s https://go.googlecode.com/files/go1.2.1.src.tar.gz | tar -v -C /usr/local -xz
RUN cd /usr/local/go/src &amp;&amp; ./make.bash --no-clean 2&gt;&amp;1
ENV PATH /usr/local/go/bin:/go/bin:$PATH
ENV GOPATH /go
RUN go get github.com/gorilla/feeds
WORKDIR /go 
CMD go version &amp;&amp; go install feed &amp;&amp; feed

It builds just fine:

sudo docker build -t ubuntu-go .

but when I run it I get a package error:

sudo docker run -v /home/rbucker/go:/go --name go ubuntu-go

The error looks like:

> src/feed/feed.go:7:2: cannot find package "github.com/gorilla/feeds" in any of:
/usr/local/go/src/pkg/github.com/gorilla/feeds (from $GOROOT)
/go/src/github.com/gorilla/feeds (from $GOPATH)

It's odd because "go install" is not installing the dependencies and while the previous "go get github.com/gorilla/feeds" completes without errors. So presumably I have a path or environment problem but all of the examples look just like this one.

PS: my code is located in /go/src/feed (feed.go)

package main

import (
    &quot;net/http&quot;
    &quot;time&quot;

    &quot;github.com/gorilla/feeds&quot;
)

. . .

UPDATE: when I performed the "go get" manually and then launched the "run" it seemed to work. So it appears that the "RUN go get" is storing my file in the ether instead of my host's volume.

sudo docker run -v /home/rbucker/go:/go --name go ubuntu-go /bin/bash

then

sudo docker run -v /home/rbucker/go:/go --name go ubuntu-go

(the files were located in my ~/go/src/githum.com and ~/go/pkg folders.)

UPDATE: It occurs to me that during the BUILD step the /go volume has not been attached to the docker image. So it's essentially assigned to nil. But then during the run the "get install" should have retrieved it's deps.

FINALLY: this works but is clearly not the preferred method:

CMD go get github.com/gorilla/feeds &amp;&amp; go version &amp;&amp; go install feed &amp;&amp; feed

notice that I performed the "go get" in the CMD rather than a RUN.

答案1

得分: 5

我通过使用以下命令解决了类似的问题:

go get ./...

来源:
https://coderwall.com/p/arxtja/install-all-go-project-dependencies-in-one-command

英文:

I solved something similar to this by using

go get ./...

Source:
https://coderwall.com/p/arxtja/install-all-go-project-dependencies-in-one-command

huangapple
  • 本文由 发表于 2014年4月19日 22:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/23171028.html
匿名

发表评论

匿名网友

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

确定