dpmopn8542
2018-12-29 10:38
浏览 1.2k

当我与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.

    点赞 评论

相关推荐 更多相似问题