Skip to content
Latchkey

docker build Command Reference

Build an image from a Dockerfile and build context.

docker build packages a build context plus a Dockerfile into a layered image. In modern Docker it invokes BuildKit by default. The final positional argument is the build context (usually .), not the Dockerfile path.

Common flags

  • -t, --tag - name and optionally tag the image as name:tag (repeatable)
  • -f, --file - path to the Dockerfile (default ./Dockerfile)
  • --build-arg - pass a build-time ARG value, e.g. NODE_ENV=production
  • --target - build only a named stage in a multi-stage Dockerfile
  • --platform - target platform, e.g. linux/amd64
  • --no-cache - ignore the layer cache and rebuild every step
  • --pull - always pull a newer version of the base image

Example

shell
docker build \
  -t myorg/app:${GITHUB_SHA} \
  -f docker/Dockerfile \
  --build-arg NODE_ENV=production \
  --target runtime \
  --pull \
  .

In CI

Tag with an immutable identifier such as the commit SHA so each build is traceable. Use --pull on scheduled builds to pick up base-image security patches, and --no-cache when you suspect a stale cached layer. Note: a frequent CI failure is "failed to compute cache key: not found" when a COPY path is outside the build context or excluded by .dockerignore.

Key takeaways

  • The last argument is the build context, not the Dockerfile; use -f for a non-default Dockerfile location.
  • Tag images with the commit SHA for reproducible, traceable CI builds.
  • --pull refreshes the base image; --no-cache forces a full rebuild.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →