Docker Config Reference (docker build)
We put out an excellent guide on using the docker build command
Example 1
docker build -t myapp:latest .
This command constructs a Docker image tagged as "myapp:latest" from the Dockerfile located in the current directory (denoted by "." as the build context). It processes each instruction in the Dockerfile sequentially, leveraging layer caching for efficiency, and is suitable for standard application packaging in development workflows.
Example 2
docker build -t webserver:1.0 --no-cache .
This command builds an image "webserver:1.0" without utilizing cached layers, forcing a complete rebuild from the base image. It is particularly useful when dependencies have changed externally or for ensuring reproducibility in continuous integration pipelines.
Example 3
docker build -t api:dev --build-arg ENV=development .
This command creates an image "api:dev" by passing a build argument "ENV=development", which can be referenced in the Dockerfile (e.g., via ARG ENV) to configure the build environment dynamically, ideal for environment-specific setups like development versus production.
Example 4
docker build -f custom.Dockerfile -t customimage:latest .
This command specifies a non-standard Dockerfile named "custom.Dockerfile" for building the image "customimage:latest", allowing flexibility in multi-Dockerfile projects where different configurations are maintained separately.
Example 5
docker build -t frontend:2.5 --target production ./frontend
This command builds the image "frontend:2.5" targeting a specific stage "production" in a multi-stage Dockerfile, optimizing the final image by discarding intermediate build artifacts, which is advantageous for reducing image size in deployment scenarios.
Example 6
docker build -t backend:latest --quiet .
This command constructs the image "backend:latest" in quiet mode, suppressing detailed build output except for the final image ID, suitable for automated scripts where minimal logging is preferred to reduce console clutter.
Example 7
docker build -t database:stable --pull .
This command builds "database:stable" while forcing a pull of the base image from the registry, ensuring the latest version is used, which is essential for incorporating security updates or patches in base images.
Example 8
docker build -t logger:v1 --network=host .
This command creates "logger:v1" using the host network during the build process, allowing access to host resources or external services without NAT, useful for builds requiring direct network connectivity like downloading from local repositories.
Example 9
docker build -t monitor:1.2 --build-arg HTTP_PROXY=http://proxy.example.com:8080 .
This command builds "monitor:1.2" with a proxy argument for HTTP requests during the build, facilitating operations behind corporate firewalls where external package downloads are proxied.
Example 10
docker build -t cacheserver:latest --force-rm .
This command constructs "cacheserver:latest" and forces removal of intermediate containers upon successful build, conserving disk space by cleaning up temporary resources automatically.
Example 11
docker build -t authmodule:3.0 --memory=2g .
This command limits the build process for "authmodule:3.0" to 2 GB of memory, preventing resource exhaustion on constrained hosts during memory-intensive builds like compilations.
Example 12
docker build -t queue:dev --cpus=2 .
This command builds "queue:dev" restricting CPU usage to 2 cores, useful for parallelizing builds on multi-core systems without overwhelming the host during concurrent operations.
Example 13
docker build -t analytics:4.1 --label com.example.version=4.1 .
This command adds a label "com.example.version=4.1" to the image "analytics:4.1", enabling metadata for versioning or filtering in orchestration tools.
Example 14
docker build -t storage:latest --output type=local,dest=./output .
This command builds "storage:latest" and exports the build result to a local directory "./output" instead of creating an image, ideal for build artifacts extraction without registry pushes.
Example 15
docker build -t proxy:2.0 --squash .
This command squashes all layers into a single layer for "proxy:2.0", reducing image size by flattening the history, though it may increase build time and is experimental in some Docker versions.
Example 16
docker build -t mailer:stable --add-host=internal.repo:192.168.1.100 .
This command adds a host entry "internal.repo:192.168.1.100" during the build of "mailer:stable", allowing resolution of internal domains for dependency fetching.
Example 17
docker build -t scheduler:1.5 --shm-size=1g .
This command sets the /dev/shm size to 1 GB for "scheduler:1.5", beneficial for builds involving shared memory operations like certain compilations or tests.
Example 18
docker build -t notifier:latest --ulimit nofile=1024:1024 .
This command adjusts the file descriptor limit to 1024 for "notifier:latest", preventing "too many open files" errors in builds with high I/O demands.
Example 19
docker build -t gateway:dev --platform linux/amd64 .
This command builds "gateway:dev" targeting the linux/amd64 platform, ensuring compatibility for cross-architecture deployments using QEMU emulation if needed.
Example 20
docker build -t processor:3.2 --progress=plain .
This command displays build progress in plain text for "processor:3.2", overriding default auto-detection for environments where rich output is unsupported.
Example 21
docker build -t renderer:latest --secret id=mysecret,src=.env .
This command mounts a secret file ".env" as "mysecret" during the build of "renderer:latest", securely injecting sensitive data without embedding it in layers.
Example 22
docker build -t optimizer:4.0 --ssh default .
This command enables SSH agent forwarding for "optimizer:4.0", allowing git clones over SSH during builds without exposing keys in the image.
Example 23
docker build -t validator:dev --cache-from previousbuild:latest .
This command uses layers from "previousbuild:latest" as cache for "validator:dev", accelerating builds in CI/CD by reusing prior artifacts.
Example 24
docker build -t indexer:2.1 --isolation=process .
This command sets process isolation for "indexer:2.1" on Windows, using Hyper-V for better compatibility in mixed environments.
Example 25
docker build -t archiver:latest --compress .
This command compresses the build context before sending to the daemon for "archiver:latest", reducing transfer time in remote build scenarios.
Example 26
docker build -t transformer:3.3 --file Dockerfile.alternate .
This specifies an alternate "Dockerfile.alternate" for "transformer:3.3", supporting variant builds without renaming files.
Example 27
docker build -t integrator:stable --tag additional:tag .
This command builds "integrator:stable" and applies an additional tag "additional:tag", facilitating multi-tagging for versioning strategies.
Example 28
docker build -t exporter:dev --build-arg BUILD_TIME="$(date +%Y%m%d)" .
This passes the current date as "BUILD_TIME" for "exporter:dev", embedding timestamps in images for traceability.
Example 29
docker build -t importer:1.8 --network customnet .
This uses a custom network "customnet" during the build of "importer:1.8", for accessing network-specific resources like private registries.
Example 30
docker build -t aggregator:latest --rm=false .
This preserves intermediate containers after building "aggregator:latest", allowing inspection of failed builds for debugging purposes.