When working with Docker, you'll encounter two different size measurements for images: compressed and uncompressed. Understanding the difference is crucial for optimizing your deployments and managing storage efficiently.
What is Uncompressed Size?
The uncompressed size represents how much disk space an image takes up once extracted to your Docker host. This is what you see when you run docker images:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-node-app latest abc123def456 2 hours ago 900MBThis metric is useful for:
- Calculating local storage requirements
- Understanding the actual disk space consumed
- Observing the effect of changes during build steps
What is Compressed Size?
The compressed size is the size of the image as stored in registries and transferred over the network. Docker automatically compresses image layers (commonly with gzip) when pushing to registries.
This is the "real" cost in terms of:
- Network bandwidth during image pulls/pushes
- Registry storage costs
- Deployment speed in production
Key Insight
Further compressing files inside your image generally has no benefit for transfer speed, since Docker already compresses layers during transmission.
Why the Difference Matters
1. Distribution Speed
Compressed size directly impacts how quickly your images can be pulled from registries. Smaller compressed sizes mean faster deployments and updates.
2. Storage Costs
Cloud registries charge based on storage. While local disk space matters for your build machines, registry costs are based on compressed sizes.
3. CI/CD Performance
Smaller images speed up your CI/CD pipelines by reducing pull and push times, leading to faster builds and deployments.
Optimization Strategies
Here are practical strategies to reduce both compressed and uncompressed sizes:
Choose Smaller Base Images
Alpine Linux-based images are significantly smaller than full distributions. For example, node:alpine is much smaller than node:latest.
Use .dockerignore Files
Exclude unnecessary files like node_modules, .git, and test files from being copied into your image.
Remove Build Tools
Install only runtime dependencies in production images. Build tools and compilers can add hundreds of megabytes unnecessarily.
Clean Up in the Same Layer
If you delete files in a later layer, the space is still consumed by the original layer. Always clean up in the same RUN command where files are created.
Measuring Your Images
Use docker inspect to get detailed size information:
docker inspect my-node-app | grep Size
"Size": 943718400, # Uncompressed size in bytes (~900MB)Tools like dive can help you analyze image layers and identify bloat by showing size contributions of each layer.
Pro Tip
Focus on optimizing compressed size for faster network transfers and reduced registry costs. Local uncompressed size is less critical unless you're constrained by disk space.
Next Steps
In the next article, we'll explore how choosing the right base image can dramatically reduce your Docker image sizes. Stay tuned!