问题描述
我在使用 skaffold
开发一些kubernetes服务方面度过了一段美好的时光,但是我周期中最长的步骤之一就是拉出容器的所有依赖项.
I'm having a ripping good time using skaffold
to develop some kubernetes services, but one of the longest steps in my cycle is pulling all the dependencies for the container.
有人对我如何最好地在一个层中缓存所有依赖项有建议吗?在docker容器中构建go二进制文件是否有最佳实践?我应该在其中进行 go 的图层吗?(此外,我还是新手,请继续阅读二进制文件,还不了解所有的花哨信息.)
Does anyone have recommendations on how I can best cache all the dependencies in a layer? Are there best practices with building go binaries inside docker containers? Should I have a layer where I do a go get
? (Also I'm a novice building go binaries, don't know all the bells and whistles yet.)
推荐答案
我在仔细研究了涉及该过程的更多内容后发现了这篇文章: 使用go mod下载来加速Golang Docker构建
I found this article after googling some more which covers the process: Using go mod download to speed up Golang Docker builds
该技巧的要点是将您的 go.mod
和 go.sum
文件复制到容器中,然后运行 go mod download
下载依赖项,然后在下一步中继续构建.
The gist of the trick is to copy your go.mod
and go.sum
files into the container, then run go mod download
to download dependencies, and then in another step continue with your build.
之所以可行,是因为除非您添加更多依赖项,否则您的 go.mod
和 go.sum
文件不会发生 not 更改.因此,当下一个 RUN
语句发生时,即 go mod download
泊坞窗知道它可以缓存该层.(来源)
This works because your the go.mod
and go.sum
files do not change unless you add more dependencies. So, when the next RUN
statement happens which is the go mod download
docker knows that that it can cache this layer. (Source)
FROM golang:1.13.9-buster as builder
# Make Build dir
RUN mkdir /build
WORKDIR /build
# Copy golang dependency manifests
COPY go.mod .
COPY go.sum .
# Cache the downloaded dependency in the layer.
RUN go mod download
# add the source code
COPY . .
# Build
RUN go build -o app
# Run
FROM debian:buster-slim
COPY --from=builder /build
WORKDIR /app
CMD ["./app"]
这篇关于进行dockerized构建,缓存依赖项拉取层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!