This document outlines several open-source tools that can help optimize Docker images for the PRD Maker application.
Docker-Slim is a tool that automatically analyzes and optimizes Docker images, making them smaller and more secure without manual intervention.
# macOS with Homebrew
brew install docker-slim
# Linux/Windows
# Download from https://github.com/docker-slim/docker-slim/releases# Build your images first
docker-compose -f docker-compose.prod.yml build
# Optimize backend image
docker-slim build --http-probe=false --include-path="/app" --include-bin="/usr/local/bin/node" prd-generator-backend:latest
# Optimize frontend image
docker-slim build --http-probe=false --include-path="/app" --include-bin="/usr/local/bin/node" prd-generator-frontend:latest- Reduces image size by up to 30x
- Improves security by removing unnecessary components
- Reduces attack surface
- Faster deployments due to smaller image sizes
Hadolint is a Dockerfile linter that helps you build best practice Docker images by checking the Dockerfile against a set of rules.
# macOS with Homebrew
brew install hadolint
# Linux/Windows
# Download from https://github.com/hadolint/hadolint/releases# Lint backend Dockerfile
hadolint backend/Dockerfile
# Lint frontend Dockerfile
hadolint frontend/Dockerfile- Using latest tag instead of specific versions
- Missing labels
- Using ADD instead of COPY
- Running apt-get without proper flags
- Not removing package manager cache
Add this step to your CI/CD pipeline:
- name: Lint Dockerfiles
run: |
hadolint backend/Dockerfile
hadolint frontend/DockerfileDive is a tool for exploring Docker image layers, helping you identify what's making your images large.
# macOS with Homebrew
brew install dive
# Linux
wget https://github.com/wagoodman/dive/releases/download/v0.10.0/dive_0.10.0_linux_amd64.deb
sudo apt install ./dive_0.10.0_linux_amd64.deb
# Windows
# Download from https://github.com/wagoodman/dive/releases# Analyze backend image
dive prd-generator-backend:latest
# Analyze frontend image
dive prd-generator-frontend:latest- Interactive layer explorer
- Shows image efficiency score
- Highlights wasted space
- Identifies large files and directories
- Use multi-stage builds (already implemented in our Dockerfiles)
- Minimize the number of layers by combining RUN commands
- Use .dockerignore to exclude unnecessary files (already implemented)
- Use specific base image versions instead of 'latest'
- Clean up in the same layer where packages are installed
- Use Alpine-based images when possible (already implemented)
- Optimize node_modules with production dependencies only
Add this to your development process:
- Write Dockerfile
- Validate with
hadolint - Build the image
- Analyze with
diveto identify optimization opportunities - Optimize using
docker-slim - Test the optimized image
- Deploy optimized image
By integrating these tools into your workflow, you can ensure the PRD Maker Docker images remain optimized, secure, and follow best practices.