微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Docker没有看到main.go

如何解决Docker没有看到main.go

我对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"]

在此先感谢您的帮助

解决方法

定义了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"]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。