Skip to main content

Traditional Docker Builds

Four Common Challenges and How BuildKit Solves Them

Updated 5 min readMarkdown

Docker transformed the way applications are built and deployed. However, the traditional Docker build process had a few limitations that often slowed down development and created security concerns. BuildKit, the modern build engine for Docker, addresses many of these challenges.

Let's look at four common problems and how BuildKit solves them.


Package Re-downloading

The Challenge

In traditional Docker builds, when a layer changes, all subsequent layers are rebuilt. This often forces package managers such as npm, pip, Maven, or apt to download dependencies again, even if they haven't changed.

This results in:

  • Longer build times
  • Increased network usage
  • Slower CI/CD pipelines

Traditional Approach

FROM node:20

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm install

COPY . .

CMD ["npm", "start"]

If the dependency layer is invalidated, npm install downloads all packages again.

How BuildKit Helps

Docker Engine 23.0 made BuildKit the default builder for Linux images. Docker Desktop already used it. docker build is an alias of docker buildx build, so you do not set DOCKER_BUILDKIT=1 or install anything to turn the engine on.

The line you add is the cache mount, not a BuildKit switch. --mount=type=cache keeps npm's download cache at /root/.npm in BuildKit's cache, outside the image. When the npm install layer has to run again, npm reuses those files instead of downloading every package. That cache is not an image layer.

BuildKit Example

# syntax=docker/dockerfile:1.7

FROM node:20

WORKDIR /app

COPY package.json package-lock.json ./

RUN --mount=type=cache,target=/root/.npm \
    npm install

COPY . .

CMD ["npm", "start"]

The # syntax line pins which Dockerfile frontend parses the file. On Engine 23.0 and later, BuildKit is already the builder, so the cache mount still works if you omit that line.

No cache flag on the command. The mount in the Dockerfile is the whole opt-in:

docker buildx build -t myapp:1.0 .

✅ Benefit: A rebuilt install reuses packages already in the BuildKit cache.


Secret Leakage

The Challenge

Many builds require access to private repositories, APIs, or internal resources. Traditionally, secrets such as tokens, passwords, or SSH keys were often passed through build arguments or copied into images.

This creates a risk because:

  • Secrets may become part of image layers.
  • Secrets can appear in build history.
  • Anyone with access to the image may be able to recover them.

Traditional Approach

FROM alpine:latest

ARG GITHUB_TOKEN

RUN git clone https://username:${GITHUB_TOKEN}@github.com/company/private-repo.git

The token may become visible in image history or build logs.

How BuildKit Helps

BuildKit reads the secret from a file on your machine and mounts it into the build container at /run/secrets/<id>. With id=github_token, that path is /run/secrets/github_token (the same rule: id=mykey mounts at /run/secrets/mykey). The mount lasts for that one RUN and is unmounted when the step finishes. BuildKit does not copy it into any layer, so it is not in the image and not in docker history.

⚠️ The mount is the part that stays out of the image. If the RUN itself writes the secret into a file in the image, that copy is a normal layer.

BuildKit Example

# syntax=docker/dockerfile:1.7

FROM alpine:latest

RUN --mount=type=secret,id=github_token \
    GITHUB_TOKEN=$(cat /run/secrets/github_token) && \
    git clone https://username:${GITHUB_TOKEN}@github.com/company/private-repo.git

id in the command must match id in the Dockerfile. src is the local file BuildKit reads:

docker buildx build --secret id=github_token,src=github_token.txt -t myapp:1.0 .

✅ Benefit: The credential exists only while that RUN is executing.


Architecture Lock

The Challenge

Traditional Docker builds are tightly coupled to the architecture of the machine performing the build.

For example:

  • Building on an x86 machine creates an x86 image.
  • Running the same image on ARM devices may require separate builds.

This becomes challenging in environments using:

  • Apple Silicon devices
  • ARM-based servers
  • Edge and IoT platforms

Traditional Approach

docker build -t myapp:latest .

The resulting image is built only for the architecture of the build host.

How BuildKit Helps

BuildKit supports multi-platform image creation through Docker Buildx, allowing developers to build images for multiple architectures from a single build process.

BuildKit Example

docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t myapp:latest \
  --push .

Docker automatically creates a multi-architecture image manifest, allowing the correct image to be pulled based on the target platform.

✅ Benefit: Greater portability and simplified cross-platform deployments.


Sequential Stage Execution

The Challenge

Traditional Docker builds process stages one after another. Even when stages are independent, they cannot be built simultaneously.

This leads to:

  • Longer build times
  • Underutilized system resources

Traditional Approach

FROM node:20 AS frontend

WORKDIR /frontend
COPY frontend/ .
RUN npm install && npm run build


FROM golang:1.23 AS backend

WORKDIR /backend
COPY backend/ .
RUN go build -o app

Even though the frontend and backend stages are independent, traditional builders process them sequentially.

How BuildKit Helps

BuildKit analyzes the dependency graph of a Dockerfile and executes independent stages in parallel whenever possible.

BuildKit Example

# syntax=docker/dockerfile:1.7

FROM node:20 AS frontend

WORKDIR /frontend
COPY frontend/ .
RUN npm install && npm run build


FROM golang:1.23 AS backend

WORKDIR /backend
COPY backend/ .
RUN go build -o app


FROM alpine:latest

COPY --from=frontend /frontend/dist ./frontend
COPY --from=backend /backend/app ./app

CMD ["./app"]

BuildKit automatically identifies that frontend and backend are independent stages and builds them in parallel. There is no parallelism flag. The same Buildx command is enough:

docker buildx build -t myapp:1.0 .

✅ Benefit: Faster builds and better utilization of CPU and system resources.


Summary

  • Package re-downloading: cache mounts keep package caches in BuildKit, so rebuilt installs reuse them.
  • Secret leakage: secret mounts expose a file only for one RUN, never in a layer.
  • Architecture lock: --platform builds one multi-architecture image manifest.
  • Sequential stages: BuildKit runs independent stages in parallel.

No comments yet. Be the first to comment.