【问题标题】:Docker doesn't see main.goDocker 看不到 main.go
【发布时间】:2020-09-24 03:35:16
【问题描述】:

我在使用 Docker 时遇到了一些问题。我的 dockerfile 没有看到 main.go。
我有那个结构项目

docker-compose.yml  
go.mod
frontend-microservice  
  -cmd  
    -app
      -main.go
  -internal
    -some folders

当我尝试启动 docker-compose 时,它​​给了我这个错误。

ERROR: Service 'frontend-microservice' failed to build: The command '/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice .' returned a non-zero code: 1

顺便说一句,dockerfile 给出了与 go.mod 相关的错误

我的 docker-compose

version: "3"
services:
    frontend-microservice:
        build:    
            context: ./frontend-microservice/
            dockerfile: Dockerfile
        ports:
            - 80:80

我的 dockerfile

# golang image where workspace (GOPATH) configured at /go.
FROM golang:alpine as builder

ADD . /go/src/frontend-microservice
WORKDIR /go/src/frontend-microservice
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
 
COPY --from=builder /frontend-microservice ./frontend-microservice
RUN mkdir ./configs 
COPY ./configs/config.json ./configs
 
EXPOSE 8080
 
ENTRYPOINT ["./frontend-microservice"]

提前感谢您的任何帮助

【问题讨论】:

  • 你的主文件在frontend-microservice/cmd/app;似乎您正在前端微服务中运行构建命令
  • go build之前添加cd cmd/app
  • P.S.不要使用ADD - use COPY
  • @colm.anseo thx,现在我可以试试了

标签: docker go microservices


【解决方案1】:

定义main()函数的文件位于cmd/app。 不要将当前工作目录更改为cmd/app,而是将cmd/app/main.go 附加到go build 命令。

您的Dockerfile 如下所示:

# golang image where workspace (GOPATH) configured at /go.
FROM golang:alpine as builder

ADD . /go/src/frontend-microservice
WORKDIR /go/src/frontend-microservice
RUN go mod download

COPY . ./

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-microservice cmd/app/main.go

FROM alpine:latest
RUN apk --no-cache add ca-certificates
 
COPY --from=builder /frontend-microservice ./frontend-microservice
RUN mkdir ./configs 
COPY ./configs/config.json ./configs
 
EXPOSE 8080
 
ENTRYPOINT ["./frontend-microservice"]

【讨论】:

  • tnx,为您提供帮助
猜你喜欢
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多