build

docker build

Builds an image from a Dockerfile and a build context. In Docker 23+ this invokes BuildKit by default and supports multi-platform, secrets, and cache mounts.

docker build [OPTIONS] PATH | URL | -

Common flags

Flag Purpose
-t, --tag Name and optionally tag the resulting image (name:tag)
-f, --file Path to Dockerfile (default: PATH/Dockerfile)
--build-arg Pass a build-time variable to ARG instructions
--no-cache Skip the layer cache and rebuild every step
--platform Target platform (e.g., linux/amd64,linux/arm64)
--target Build a specific stage in a multi-stage Dockerfile
--secret Expose a secret to the build without baking it into layers

Examples

docker build -t myapp:1.0 .

Standard build from current directory

docker build -f Dockerfile.prod --target runtime -t myapp:prod .

Build a named stage from a custom Dockerfile

docker build --platform linux/amd64,linux/arm64 -t user/img:latest --push .

Multi-arch build (requires buildx driver)

docker build --secret id=npmrc,src=$HOME/.npmrc -t myapp .

Use a build secret safely

Gotcha

Multi-platform builds require a buildx builder with a supported driver — the default docker driver only builds for the host arch. Sending huge build contexts is slow; use .dockerignore aggressively.

Related commands

← All Docker commands · Docker vs Kubernetes · Docker vs VMs