docker-compose无法找到包。

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

docker-compose cannot find package

问题

我正在使用GO编写一个简单的应用程序,我有以下文件夹结构:

docker-compose无法找到包。

docker-compose.yml文件的内容如下:

version: '2'
services:
  db:
    image: rethinkdb:latest
    ports:
      - "38080:8080"
      - "38015:28015"
      - "39015:29015"
  api:
    image: golang:1.8-alpine
    volumes:
      - .:/go/src/test_server/
    working_dir: /go/src/test_server
    command: go run server.go
    container_name: test_server
    ports:
      - "8085:8085"
    links:
      - db
    tty: true

每次我运行docker-compose up时,都会收到以下错误消息:

> test_server | controllers/users.go:4:3: cannot find package
> "/go/src/test_server/vendor/github.com/gin-gonic/gin" in any of:
> test_server |
> /usr/local/go/src/
/go/src/test_server/vendor/github.com/gin-gonic/gin
> (from $GOROOT) test_server |
> /go/src/_/go/src/test_server/vendor/github.com/gin-gonic/gin (from
> $GOPATH)

它指的是controllers包。我正在使用github.com/kardianos/govendor来管理我的包依赖。你知道出了什么问题吗?

英文:

I'm writing a simple app in GO and I have this folder structure

docker-compose无法找到包。

The docker-compose.yml file content is:

version: '2'
services:
  db:
    image: rethinkdb:latest
    ports:
      - "38080:8080"
      - "38015:28015"
      - "39015:29015"
  api:
    image: golang:1.8-alpine
    volumes:
      - .:/go/src/test_server/
    working_dir: /go/src/test_server
    command: go run server.go
    container_name: test_server
    ports:
      - "8085:8085"
    links:
      - db
    tty: true

Everytime I run docker-compose up I receive this error message:

> test_server | controllers/users.go:4:3: cannot find package
> "/go/src/test_server/vendor/github.com/gin-gonic/gin" in any of:
> test_server |
> /usr/local/go/src/
/go/src/test_server/vendor/github.com/gin-gonic/gin
> (from $GOROOT) test_server |
> /go/src/_/go/src/test_server/vendor/github.com/gin-gonic/gin (from
> $GOPATH)

It's referring to the controllers package. I'm using github.com/kardianos/govendor to vendor my packages. Do you know what's going on?

答案1

得分: 4

经过多个小时的努力,我终于修复了它。
结果是我使用的 Docker Golang 版本不包含 git。我应该使用 golang:1.8。

我修改了我的 Dockerfile,现在它完美运行:

FROM golang:1.8

RUN go get github.com/gin-gonic/gin

WORKDIR /go/src/app
COPY . .

RUN go install -v

CMD ["app"]
英文:

After many hours I finally could fix it.
Resulted that I was using a docker golang version that it doesn't have git included. I should use golang:1.8

I modified my Dockerfile like this and now it works like a charm

FROM golang:1.8

RUN go get github.com/gin-gonic/gin

WORKDIR /go/src/app
COPY . .

RUN go install -v

CMD ["app"]

答案2

得分: 0

你需要告诉go在哪里找到这些包:

api:
  ...
  environment:
    - GOPATH=/go/src/test_server

或者使用已安装适当包的Dockerfile(推荐)。

英文:

You need to tell go where find the packages:

api:
  ...
  environment:
    - GOPATH=/go/src/test_server

Or have a Dockerfile with the proper packages installed (recommended)

答案3

得分: 0

我认为这是因为你的更新代码正在运行go install,而不是你的旧代码运行的go run

你需要将额外的golang包安装到你的应用程序调用的供应商目录中。

英文:

I think it's because your updated code is running go install, not go run which your old code was running.

You needed to install the extra golang packages into the vendor directory that you are calling from your app.

huangapple
  • 本文由 发表于 2017年5月27日 05:43:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/44210502.html
匿名

发表评论

匿名网友

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

确定