当我将 go 与 docker compose 一起使用时,出现“No Go files in...”错误

Cel*_*rim 3 go docker docker-compose

我在 Ubuntu 16.04 上安装了 Go。这是我的 GOPATH=/home/{用户名}/work。

\n\n

我在 /home/{username}/work/src 中创建了一个项目。

\n\n

这是我的项目文件夹层次结构。

\n\n
project-name\n   services\n       configuration\n           api\n               main.go\n           Dockerfile\n       bff\n           api\n               main.go\n           Dockerfile\n   docker-compose.yml\n   favicon.ico\n   README.md\n
Run Code Online (Sandbox Code Playgroud)\n\n

我可以使用 dockerfile 构建和运行,但无法使用 docker-compose 构建和运行。

\n\n

我找不到任何解决方案。

\n\n

配置服务dockerfile:

\n\n
FROM golang:1.11.1-alpine3.8 as builder\n\nRUN apk update && apk add git && go get gopkg.in/natefinch/lumberjack.v2\n\nRUN mkdir -p /go/src/project-name/services/configuration\nRUN CGO_ENABLED=0\nRUN GOOS=linux\nADD . /go/src/project-name/services/configuration\nENV GOPATH /go\nWORKDIR /go/src/project-name/services/configuration/api\nRUN go get\nRUN go build\n\nFROM alpine\nRUN apk update\nRUN apk add curl\n\nRUN mkdir -p /app\nCOPY --from=builder /go/src/project-name/services/configuration/api/ /app/\nRUN chmod +x /app/api\nWORKDIR /app\nEXPOSE 5001\nENTRYPOINT ["/app/api"]\n
Run Code Online (Sandbox Code Playgroud)\n\n

它与 dockerfile 一起使用。

\n\n

这是我的 docker-compose 文件:

\n\n
version: \'3.4\'\n\nservices:\n   bff:\n      image: project-name/bff:${TAG:-latest}\n      build:\n           context: .\n           dockerfile: services/bff/Dockerfile\n      ports:\n          - "5000:5000"\n      container_name: bff \n      depends_on:\n         - configuration\n\n   configuration:\n       image: project-name/configuration:${TAG:-latest}\n       build:\n            context: .\n            dockerfile: services/configuration/Dockerfile    \n       ports:\n            - "5001:5001"\n       container_name: configuration\n
Run Code Online (Sandbox Code Playgroud)\n\n

它不起作用。

\n\n

当\xe2\x80\x9crun go get\xe2\x80\x9d命令运行时,出现错误,错误是:

\n\n
 can\'t load package: package project-name/services/configuration/api: no Go files in /go/src/project-name/services/configuration/api\n ERROR: Service \'configuration\' failed to build: The command \'/bin/sh -c go get\' returned a non-zero code: 1\n
Run Code Online (Sandbox Code Playgroud)\n

Dav*_*aze 5

在你的 Dockerfile 中,你说

ADD . /go/src/project-name/services/configuration
Run Code Online (Sandbox Code Playgroud)

它期望主机上的构建上下文目录包含源文件。但你的docker-compose.yml文件说

build:
    context: .
    dockerfile: services/configuration/Dockerfile    
Run Code Online (Sandbox Code Playgroud)

其中上下文目录是源代码控制树的根,而不是您尝试构建的特定 Go 源目录。如果你将其更改为

build:
    context: services/configuration
    # Default value of "dockerfile: Dockerfile" will be right
Run Code Online (Sandbox Code Playgroud)

它可能会工作得更好。

在普通的 Docker 命令中,您当前的docker-compose.yml文件相当于

cd $GOPATH/src/project-name
docker build -f services/configuration/Dockerfile .
Run Code Online (Sandbox Code Playgroud)

但你可能实际上正在跑步

cd $GOPATH/src/project-name/services/configuration
docker build .
Run Code Online (Sandbox Code Playgroud)

当前目录是什么目录很重要。