gpt4 book ai didi

docker - Docker没有看到main.go

转载 作者:行者123 更新时间:2023-12-01 22:10:32 25 4
gpt4 key购买 nike

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

关于docker - Docker没有看到main.go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64039155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com