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

当我与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 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)