登录
首页 >  Golang >  Go问答

错误信息:“No Go files in...” 出现,当我结合使用 go 与 docker compose

来源:stackoverflow

时间:2024-03-03 13:21:24 151浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《错误信息:“No Go files in...” 出现,当我结合使用 go 与 docker compose 》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

我在 ubuntu 16.04 上安装了 go。这是我的 gopath=/home/{username}/work。

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

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

project-name
   services
       configuration
           api
               main.go
           dockerfile
       bff
           api
               main.go
           dockerfile
   docker-compose.yml
   favicon.ico
   readme.md

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

我找不到任何解决方案。

配置服务dockerfile:

from golang:1.11.1-alpine3.8 as builder

run apk update && apk add git && go get gopkg.in/natefinch/lumberjack.v2

run mkdir -p /go/src/project-name/services/configuration
run cgo_enabled=0
run goos=linux
add . /go/src/project-name/services/configuration
env gopath /go
workdir /go/src/project-name/services/configuration/api
run go get
run go build

from alpine
run apk update
run apk add curl

run mkdir -p /app
copy --from=builder /go/src/project-name/services/configuration/api/ /app/
run chmod +x /app/api
workdir /app
expose 5001
entrypoint ["/app/api"]

它与 dockerfile 一起使用。

这是我的 docker-compose 文件:

version: '3.4'

services:
   bff:
      image: project-name/bff:${tag:-latest}
      build:
           context: .
           dockerfile: services/bff/dockerfile
      ports:
          - "5000:5000"
      container_name: bff 
      depends_on:
         - configuration

   configuration:
       image: project-name/configuration:${tag:-latest}
       build:
            context: .
            dockerfile: services/configuration/dockerfile    
       ports:
            - "5001:5001"
       container_name: configuration

没有成功。

当“run go get”命令运行时,出现错误,错误为:

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

解决方案


在你的 dockerfile 中,你说

add . /go/src/project-name/services/configuration

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

build:
    context: .
    dockerfile: services/configuration/dockerfile

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

build:
    context: services/configuration
    # default value of "dockerfile: dockerfile" will be right

它可能会工作得更好。

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

cd $gopath/src/project-name
docker build -f services/configuration/dockerfile .

但你可能实际上正在跑步

cd $GOPATH/src/project-name/services/configuration
docker build .

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

到这里,我们也就讲完了《错误信息:“No Go files in...” 出现,当我结合使用 go 与 docker compose 》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>