size command validates that a container image’s total size and number of layers are within acceptable limits. This helps control image bloat and optimize container startup time.
Usage
Description
Thesize command calculates the total uncompressed size of all image layers and counts the number of layers. Both metrics are validated against configurable thresholds.
This check helps:
- Control image bloat and disk usage
- Optimize container startup time
- Reduce network transfer time
- Identify images that need optimization
- Enforce layer count limits (Docker has a 127 layer maximum)
Flags
Maximum image size in megabytes (MB). Images larger than this value will fail validation.Short form:
-mMaximum number of layers. Images with more layers will fail validation.Short form:
-yOutput format:
text or jsonShort form: -oColor output mode:
auto, always, neverSet log level: trace, debug, info, warn, error, fatal, panic
Examples
Basic usage with default limits
Custom size and layer limits
Strict limits for production
Check OCI layout
JSON output
Output
Text Format
When validation passes:JSON Format
Exit Codes
| Code | Meaning | Example |
|---|---|---|
| 0 | Image size and layers within limits | 187 MB with 6 layers, limits are 500 MB and 20 layers |
| 1 | Image exceeds size or layer limit | 600 MB image with max-size 500 |
| 2 | Execution error | Image not found, invalid flag value |
Configuration
When using theall command, configure size validation in your config file:
Implementation Details
- Calculates uncompressed size (not registry compressed size)
- Counts all layers in the image (including base image layers)
- Size is the sum of all layer sizes in bytes
- Validates both size and layer count independently
- Fails if either limit is exceeded
- Includes per-layer size breakdown in details
Common Issues
Large images
Images exceeding size limits often have these causes:- Build artifacts or caches not cleaned up
- Development tools included in production image
- Large dependencies or data files
- Missing
.dockerignorefile
- Use multi-stage builds to exclude build dependencies
- Clean up package manager caches (
apt-get clean,yum clean all) - Use smaller base images (alpine, distroless)
- Add proper
.dockerignoreentries
Too many layers
- Each
RUN,COPY,ADDcreates a new layer - Not combining related commands
- Deep inheritance chain (many
FROMstatements)
- Combine related
RUNcommands with&& - Use multi-stage builds to reduce final layer count
- Flatten images if necessary
Best Practices
Set size limits based on your deployment environment (smaller for edge, larger for datacenter)
Keep layer count under 20 for most applications
Use multi-stage builds to reduce final image size
Regularly audit layer contents with
docker historyUse
.dockerignore to exclude unnecessary files