docker-build
Build Docker images from a Dockerfile
TLDR
Build a Docker image using the Dockerfile in the current directory
Build a Docker image from a Dockerfile at a specified URL
Build a Docker image and tag it
Build a Docker image with no build context
Do not use the cache when building the image
Build a Docker image using a specific Dockerfile
Build with custom build-time variables
SYNOPSIS
docker build [OPTIONS] PATH | URL | -
PARAMETERS
--add-host list
Add a custom host-to-IP mapping (host:ip)
--build-arg list
Set build-time variables
--cache-from list
Images to consider as cache sources
--cgroup-parent string
Optional parent cgroup for the container
--compress
Compress the build context using gzip
--cpu-period int
Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int
Limit the CPU CFS quota
--cpuset-cpus string
CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string
Memory nodes in which to allow execution (0-3, 0,1). Only effective on NUMA systems.
--disable-content-trust
Skip image verification
--file string, -f string
Name of the Dockerfile (default: Dockerfile)
--force-rm
Always remove intermediate containers
--iidfile string
Write the image ID to the file
--isolation string
Container isolation technology
--label list
Set metadata for an image
--memory bytes
Memory limit
--memory-swap bytes
Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string
Set the networking mode for the RUN instructions during build (default "default")
--no-cache
Do not use cache when building the image
--pull
Always attempt to pull a newer version of the image
--quiet, -q
Suppress the build output and print image ID on success
--rm
Remove intermediate containers after a successful build (default true)
--security-opt list
Security options
--shm-size bytes
Size of /dev/shm
--squash
Squash newly built layers into a single new layer
--stream
Stream attaches to server to negotiate build context
--tag string, -t string
Name and optionally a tag in the 'name:tag' format
--target string
Set the target build stage to build
--ulimit ulimit
Ulimit options
DESCRIPTION
The docker build command builds Docker images from a Dockerfile and a 'context'. The context is a set of files located in the specified PATH or URL. The build process uses the instructions in the Dockerfile to create a layered image. Each instruction in the Dockerfile forms a layer in the image. Docker optimizes builds by caching these layers, reusing them if the instructions and context remain unchanged. This significantly speeds up subsequent builds. The build context is crucial as it provides the files that are copied into the image during the build process, typically using the COPY or ADD instructions in the Dockerfile.
It's important to keep the build context as small as possible to reduce the amount of data transferred to the Docker daemon, resulting in faster build times. Ignoring unnecessary files using a `.dockerignore` file is highly recommended.
CAVEATS
Building images requires elevated privileges (root or membership in the docker group). The build context is uploaded to the Docker daemon, so a large context can significantly increase build times.
BUILD CONTEXT AND .DOCKERIGNORE
The build context is a directory on your local machine that is used as the source for building the Docker image. It's critical to manage the build context effectively. The `.dockerignore` file allows you to exclude files and directories from the build context, reducing the size of the data transferred to the Docker daemon and improving build performance. A smaller build context translates to faster builds.
DOCKERFILE BEST PRACTICES
Following Dockerfile best practices can significantly improve build performance, image size, and security. Some key best practices include: using multi-stage builds to reduce image size, ordering instructions logically to leverage caching effectively, using specific image tags to avoid unexpected updates, and minimizing the number of layers by combining instructions when possible.
MULTI-STAGE BUILDS
Multi-stage builds are a powerful feature that allows you to use multiple FROM statements in a single Dockerfile. Each FROM instruction starts a new build stage. You can copy artifacts from one stage to another, allowing you to use different base images and tools in different stages. This is particularly useful for reducing the final image size by including only the necessary runtime dependencies.
SEE ALSO
docker(1), dockerfile(5), docker-push(1)