How to use docker build
The docker build
command is used to build Docker images from a Dockerfile. Let's cover the essential usage and most important options.
Basic Usage
The basic syntax is:
docker build [OPTIONS] PATH | URL | -
This tells Docker to build an image using the Dockerfile in the specified PATH or URL. The .
at the end means "current directory".
Most Important Options
Here are the most commonly used options:
-t, --tag
Tag your image (give it a name):
docker build -t myapp:latest .
-f, --file
Specify a different Dockerfile name:
docker build -f custom.Dockerfile .
--no-cache
Force a fresh build without using cached layers:
docker build --no-cache .
--build-arg
Pass variables to the build process:
docker build --build-arg VERSION=1.0 .
--platform
Build for specific platforms:
docker build --platform linux/amd64,linux/arm64 .
--pull
Always pull the latest base images:
docker build --pull .
Common Use Cases
- Basic build with tag:
docker build -t myapp:1.0 .
- Build with multiple tags:
docker build -t myapp:latest -t myapp:1.0 .
- Build with build arguments:
docker build --build-arg ENV=production -t myapp:prod .
Conclusion
While docker build
has many options, these are the ones you'll use most often. They cover the essential needs for building Docker images: naming your images, specifying build variables, and controlling the build cache.