Dockerfile正在假装并行运行,但实际上并没有。

huangapple go评论61阅读模式
英文:

Dockerfile is pretending to run in parallel, but it is not

问题

我尝试构建依赖于同一依赖项的三个应用程序,并在单个 Dockerfile 中并行运行它们。使用我在这里找到的答案,我成功地让它假装并行运行,但实际上并没有。

我知道这不是最佳实践,但我需要使用一个 Dockerfile 构建三个独立的应用程序。如果您有使 Docker 并行运行或满足上述要求的替代解决方案,我将非常乐意听取!

总体目标:加快部署这三个应用程序时的构建过程。

原因:随着这些应用程序变得更加复杂,构建时间会增加。

我的代码

这是我的 Dockerfile 片段:

FROM golang:1.19-alpine3.17 AS base

RUN apk add bash ca-certificates git gcc g++ libc-dev make

COPY go.mod .
COPY go.sum .

RUN go mod download

FROM base AS builder1
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -o path/app1 ./dest/app1

FROM base AS builder2
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -o path/app2 ./dest/app2

FROM base AS builder3
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -o path/app3 ./dest/app3

FROM builder1 AS finalbuilder
COPY --from=builder1 path/to/app1 /bin/app1
COPY --from=builder2 path/to/app2 /bin/app2
COPY --from=builder3 path/to/app3 /bin/app3

我使用以下命令构建这个 Dockerfile:

DOCKER_BUILDKIT=1 docker build --network=host --target=finalbuilder -t $(DOCKER_BUILDER_IMAGE) -f path/Dockerfile .

结果

它运行成功,并假装同时构建了所有三个应用程序(控制台上的计时器同时计数)。然而,实际上花费的时间揭示了问题。单独构建每个应用程序大约需要30秒。使用上述方法构建时,构建所有三个应用程序大约需要90秒。Docker 明显仍然按顺序构建它们,只是假装没有这样做。

英文:

I am trying to build 3 apps that rely on the same dependencies in parallel inside of a single Dockerfile. Using the answer I found here I got it to the point where it pretends to run in parallel, but it is not.

I am aware this is not the best practice way to accomplish this goal, but I need 3 separate applications built with only 1 Dockerfile. If you have a solution that either makes Docker run in parallel, or an alternate solution that satisfies the above requirements, I would love to hear it!

Overall goal: Speed up the build process when the three apps are deployed.

Why: As these apps get more complicated, the build time will go up.

My Code

Here is a clip of my Dockerfile:

FROM golang:1.19-alpine3.17 AS base

RUN apk add bash ca-certificates git gcc g++ libc-dev make

COPY go.mod .
COPY go.sum .

RUN go mod download

FROM base AS builder1
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -o path/app1 ./dest/app1

FROM base AS builder2
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -o path/app2 ./dest/app2

FROM base AS builder3
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -o path/app3 ./dest/app3

FROM builder1 AS finalbuilder
COPY --from=builder1 path/to/app1 /bin/app1
COPY --from=builder2 path/to/app2 /bin/app2
COPY --from=builder3 path/to/app3 /bin/app3

I build this Dockerfile using
DOCKER_BUILDKIT=1 docker build --network=host --target=finalbuilder -t $(DOCKER_BUILDER_IMAGE) -f path/Dockerfile .

Result

It runs successfully, and pretends to build all 3 apps at the same time. (Timer counts up in console on the three builds at the same time) However, the time it takes to do so gives it away. Each app by itself takes roughly 30 seconds to build. When using this method described above, it takes roughly 90 seconds to build all 3. Docker is clearly still building them in order, while pretending not to.

答案1

得分: 0

为什么不运行三个简单的Docker容器?
> 我需要使用仅一个Dockerfile构建3个单独的应用程序。

所以仍然可以使用不同的参数来创建一个Dockerfile,这样你可以创建3个容器 Dockerfile正在假装并行运行,但实际上并没有。
另一种方法是运行一个简单的脚本,它将存在于容器内并在不同的线程中执行命令。
或者
使用来自此处的一些Bash脚本。
思路是你可以从一个构建器中并行运行三个构建命令,这里似乎不需要使用RUN命令。

英文:

Why to not run three simple docker contrainers?
> I need 3 separate applications built with only 1 Dockerfile

So it still could be one dockerfile with different params, so, you could just create 3 containers Dockerfile正在假装并行运行,但实际上并没有。
Another way - is to run a simple script that will live inside container and will execute commands in a different threads.
OR
use some bash from here.
The idea is that you could run in parallel three build commands from one builder, and RUN looks not necessary here.

huangapple
  • 本文由 发表于 2023年6月8日 06:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定