|
| 1 | +# Docker Image Size Optimization Summary |
| 2 | + |
| 3 | +## Problem |
| 4 | +The original Docker images were unnecessarily large due to including all build dependencies, full JDK, and build artifacts in the final runtime image. |
| 5 | + |
| 6 | +## Solution |
| 7 | +Implemented multi-stage Docker builds with the following optimizations: |
| 8 | + |
| 9 | +### 1. Multi-Stage Architecture |
| 10 | +**Before (Single-stage):** |
| 11 | +``` |
| 12 | +FROM almalinux:9 |
| 13 | +├── Install all build tools (gcc, make, bison, flex, etc.) |
| 14 | +├── Install full JDK (java-11-openjdk-devel) |
| 15 | +├── Download and compile software |
| 16 | +└── Final image contains everything |
| 17 | +``` |
| 18 | + |
| 19 | +**After (Multi-stage):** |
| 20 | +``` |
| 21 | +# Build Stage |
| 22 | +FROM almalinux:9 AS builder |
| 23 | +├── Install build tools |
| 24 | +├── Download and compile software |
| 25 | +└── Stage files for copying |
| 26 | +
|
| 27 | +# Runtime Stage |
| 28 | +FROM almalinux:9 |
| 29 | +├── Install minimal runtime (headless JRE only) |
| 30 | +├── Copy built files from builder |
| 31 | +└── Minimal final image |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. Dependency Optimization |
| 35 | +- **Build stage only**: gcc, make, bison, flex, automake, autoconf, diffutils, gettext, java-11-openjdk-devel |
| 36 | +- **Runtime stage only**: java-11-openjdk-headless (much smaller than full JDK) |
| 37 | + |
| 38 | +### 3. Cleanup Optimizations |
| 39 | +- Combined RUN commands to reduce Docker layers |
| 40 | +- Added `dnf clean all` to remove package manager caches |
| 41 | +- Added `rm -rf /var/cache/dnf/*` for additional cache cleanup |
| 42 | +- Source directories removed after compilation (`rm -rf /root/opensourcecobol4j-*`) |
| 43 | + |
| 44 | +### 4. Efficient File Management |
| 45 | +- Used `DESTDIR=/tmp/install` for staged installation |
| 46 | +- Only necessary runtime files copied with `COPY --from=builder /tmp/install/usr/ /usr/` |
| 47 | +- Build artifacts and intermediate files excluded from final image |
| 48 | + |
| 49 | +## Expected Size Reduction: 60-80% |
| 50 | + |
| 51 | +### Components Eliminated from Final Image: |
| 52 | +- **Build tools**: ~200-300MB (gcc, make, bison, flex, automake, etc.) |
| 53 | +- **Full JDK vs headless**: ~100-150MB (java-11-openjdk-devel → java-11-openjdk-headless) |
| 54 | +- **Package caches**: ~50-100MB (dnf cache, /var/cache/dnf/*) |
| 55 | +- **Source code & artifacts**: ~50-100MB (downloaded tarballs, build directories) |
| 56 | + |
| 57 | +### Total Estimated Reduction: ~400-650MB |
| 58 | + |
| 59 | +## Files Modified |
| 60 | +- `Dockerfile` - Multi-stage build for opensourcecobol4j + Open COBOL ESQL 4J |
| 61 | +- `utf8.Dockerfile` - Multi-stage build for opensourcecobol4j with UTF-8 support |
| 62 | +- `OPTIMIZATION.md` - Documentation of optimization techniques |
| 63 | + |
| 64 | +## Functionality Preserved |
| 65 | +Both optimized images maintain full functionality: |
| 66 | +- All required binaries and libraries included |
| 67 | +- Sample programs included |
| 68 | +- Environment variables and classpath correctly configured |
| 69 | +- Same user experience and capabilities as original images |
0 commit comments