docker - 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"]
推荐阅读
- php - 调用未定义的方法 Illuminate\Database\Query\Builder::fireCustomModelEvent()
- postgis - 如何按最近点查询postgis数据并只返回该点的结果?
- c# - 如何使用 SQl、EF 和 linQ 从 C# 模型中获取数据
- python-3.x - python - 如何使用python在pandas数据框中的第一个连续重复值后删除?
- reactjs - 如何使用 redux 和 redux-thunk 将获取的数据从服务器保存到组件状态?
- c# - 如何定义一个自定义标签来过滤基于类型的每个列表
- python - 来自自定义 ansible 模块的调试信息
- mysql - 选择另一列而不是与 GROUP BY 一起使用
- python - 使用 Django 进行日期过滤——如何将开始和结束日期发送到我的视图?
- wcf - WCF 肥皂格式化程序