gpt4 book ai didi

docker - 使用 mono repo 在 Docker 容器中包含 Go 依赖项

转载 作者:IT王子 更新时间:2023-10-29 02:07:00 28 4
gpt4 key购买 nike

我有一个具有该结构的单一存储库。

mono-repo
- serviceA
- main.go
- Dockerfile
-serviceB
- main.go
- Dockerfile
go.mod
go.sum

serviceA 中的 Dockerfile 包含以下代码。

FROM golang

ENV GO111MODULE=on

WORKDIR /app

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

ENTRYPOINT ["/app/serviceA"]

我想构建 Docker 镜像并在容器中包含来 self 的 mono-repo 根目录的依赖项,我目前收到一条错误消息,指出它在我运行时找不到任何依赖包

docker build -t serviceA .

除非我在 serviceA 中放置一个 go.mod,否则我看不到实现我想要的东西的好方法。通过在服务中放置一个 go.mod,感觉我正在失去服务在 repo 中共享依赖项的优势。

最佳答案

By placing a go.mod inside the service it feels like I'm losing the advantage of services sharing dependencies within the repo.

然而,这是一种可见的方法 herethere ,其中 COPY go.mod .(和 COPY go.sum .)后跟 RUN go mod download

#This is the ‘magic’ step that will download all the dependencies that are specified in 
# the go.mod and go.sum file.
# Because of how the layer caching system works in Docker, the go mod download
# command will _ only_ be re-run when the go.mod or go.sum file change
# (or when we add another docker instruction this line)
RUN go mod download

关于docker - 使用 mono repo 在 Docker 容器中包含 Go 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55786159/

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