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

标签 docker go golang-migrate

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

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/

相关文章:

docker - 新构建后的Docker中的镜像和容器

docker - nginx + docker : http to https redirection

docker - 将主机环境变量与Docker远程API结合使用

laravel - 在 Laravel 项目中,命令 "./vendor/bin/sail up"在 -q"中返回 "unknown shorthand flag: ' q'

go - 推迟 : usage in case of long running functions?

go - 当请求正文是对象数组时,绑定(bind)验证不起作用

Golang解码没有键/值的数组来构造

google-app-engine - 是否在没有App Engine独立SDK的情况下检测Go中的开发App Server?

sockets - 如何从Golang中的socket io获取ip地址