docker build
The docker build command is used to build an image from a Dockerfile. This command reads the instructions from a Dockerfile and creates a Docker image based on those instructions.
Basic Syntax
Key Options and Parameters
-t, --tag: Name and optionally a tag in the 'name:tag' format.
docker build -t myapp:latest .-f, --file: Name of the Dockerfile (default is 'PATH/Dockerfile').
docker build -f Dockerfile.dev -t myapp:dev .--build-arg: Set build-time variables.
docker build --build-arg VERSION=1.0 -t myapp:latest .--no-cache: Do not use cache when building the image.
docker build --no-cache -t myapp:latest .--rm: Remove intermediate containers after a successful build (default is true).
docker build --rm -t myapp:latest .--pull: Always attempt to pull a newer version of the image.
docker build --pull -t myapp:latest .
Examples
Building an Image from a Dockerfile
docker build -t myapp:latest .This command builds an image named
myappwith the taglatestfrom the Dockerfile in the current directory.Specifying a Different Dockerfile
docker build -f Dockerfile.dev -t myapp:dev .This command builds an image using
Dockerfile.devinstead of the defaultDockerfile.Using Build Arguments
docker build --build-arg VERSION=1.0 -t myapp:latest .This command sets a build-time variable
VERSIONto1.0.Building Without Cache
docker build --no-cache -t myapp:latest .This command builds the image without using any cached layers.
Pulling the Latest Base Image
docker build --pull -t myapp:latest .This command ensures that the latest version of the base image is used.
Conclusion
The docker build command is essential for creating Docker images from a Dockerfile. Understanding its options and parameters allows you to effectively build and manage Docker images based on your requirements.