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.