To optimize image size for production:

  • Use lightweight base images like alpine or scratch.

  • Use multi-stage builds to exclude build-time dependencies.

  • Remove unnecessary files (logs, cache, docs) after installation steps.

  • Minimize the number of layers in the Dockerfile.

  • Combine commands using && to reduce layers.

  • Avoid adding unnecessary tools and debug packages in production images.

Read this article for more details: How to Optimize Docker Images for Smaller, Faster Deployments