LinuxCommandLibrary

docker-build

TLDR

Build an image from Dockerfile

$ docker build -t [image_name] .
copy
Build with specific Dockerfile
$ docker build -f [Dockerfile.dev] -t [image_name] .
copy
Build with build arguments
$ docker build --build-arg [VAR=value] -t [image_name] .
copy
Build without cache
$ docker build --no-cache -t [image_name] .
copy
Build for specific platform
$ docker build --platform [linux/amd64] -t [image_name] .
copy
Build multi-platform image (buildx)
$ docker buildx build --platform [linux/amd64,linux/arm64] -t [image_name] .
copy
Build and push to registry
$ docker build -t [registry/image:tag] --push .
copy

SYNOPSIS

docker build [options] path|url

DESCRIPTION

docker build creates Docker images from a Dockerfile and context (files available during build). The Dockerfile contains instructions for assembling the image layer by layer.
The build context is sent to the Docker daemon, which processes Dockerfile instructions sequentially. Each instruction creates a layer; layers are cached and reused when unchanged, speeding up subsequent builds.
Modern builds use BuildKit (enabled by default in recent Docker versions), providing improved performance, better caching, and features like secrets and SSH forwarding.

PARAMETERS

-t, --tag name:tag

Name and optionally tag the image.
-f, --file path
Path to Dockerfile (default: PATH/Dockerfile).
--build-arg key=value
Build-time variables.
--no-cache
Don't use cache when building.
--pull
Always pull newer base image.
--target stage
Build specific stage in multi-stage Dockerfile.
--platform platform
Target platform (linux/amd64, linux/arm64).
--progress type
Progress output: auto, plain, tty.
--secret id=secret
Secret to expose to build.
--ssh socket
SSH agent socket to expose.
-q, --quiet
Suppress build output.
--push
Push image after build (buildx).

CAVEATS

Large build contexts slow builds; use .dockerignore to exclude unnecessary files. Layer caching depends on instruction order; put frequently changing instructions last. Multi-platform builds require buildx and may need emulation or cross-compilation setup.

HISTORY

Docker build has been a core Docker feature since the initial release in 2013. BuildKit, a next-generation builder with improved performance and features, was introduced in 2017 and became the default in Docker 23.0 (2023). The buildx plugin extends build capabilities for multi-platform images and advanced build scenarios.

SEE ALSO

Copied to clipboard