Docker 没看到 main.go
Docker doesn't see main.go
我在使用 Docker 时遇到一些问题。我的 docker 文件没有看到 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
顺便说一句 docker文件给出与 go.mod
相关的错误
我的 docker-撰写
version: "3"
services:
frontend-microservice:
build:
context: ./frontend-microservice/
dockerfile: Dockerfile
ports:
- 80:80
我的docker文件
# 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。
我有那个结构项目
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
顺便说一句 docker文件给出与 go.mod
相关的错误我的 docker-撰写
version: "3"
services:
frontend-microservice:
build:
context: ./frontend-microservice/
dockerfile: Dockerfile
ports:
- 80:80
我的docker文件
# 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"]