dpmopn8542 2018-12-29 10:38
浏览 1784
已采纳

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

I installed Go on Ubuntu 16.04. This is my GOPATH=/home/{username}/work.

I created a project into /home/{username}/work/src.

This is my project folder hierarchy.

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

I can build and run with my dockerfile but I can't build and up with docker-compose.

I couldn't find any solution.

Configuration service 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"]

It works with dockerfile.

This is my docker-compose file:

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

It didn't work.

When the “run go get” command runs, it gives an error, the error is:

 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
  • 写回答

1条回答 默认 最新

  • doujiaoben28596 2018-12-29 12:10
    关注

    In your Dockerfile, you say

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

    which expects the build context directory on the host to contain the source files. But your docker-compose.yml file says

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

    where the context directory is the root of your source control tree, not the specific Go source directory you're trying to build. If you change this to

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

    it will likely work better.

    In plain Docker commands, your current docker-compose.yml file says the equivalent of

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

    But you're probably actually running

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

    and what directory is the current directory matters.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!