docker build
Build an image from a Dockerfile. The build context is typically the directory containing the Dockerfile.
Syntax
docker build [OPTIONS] PATH | URL | -
Examples
Example 1
docker build -t myapp:latest .
Build from current directory and tag as myapp:latest.
Example 2
docker build -f Dockerfile.prod -t myapp:v1.2 .
Use a non-default Dockerfile and tag with version.
Example 3
docker build --no-cache -t myapp .
Build without using cache (full rebuild).
Common flags
| Flag | Description |
|---|---|
| -t, --tag | Name and optionally tag (name:tag) |
| -f, --file | Path to Dockerfile |
| --no-cache | Do not use cache when building |
| --build-arg | Set build-time variable |
Tips & best practices
- •Keep Dockerfile in project root for smaller build context.
- •Use .dockerignore to exclude files from the build context.
- •Multi-stage builds reduce final image size.