• Explore
  • About Us
  • Log In
  • Get Started
  • Explore
  • About Us
  • Log In
  • Get Started

Building Minimal Docker Images

In this exercise, you will focus on optimizing a Docker image by minimizing its size. Using the Dockerfile from the previous exercise, "Containerizing a Legacy Application," you will learn how to reduce the image size while ensuring that the application still runs correctly. This optimization is crucial for faster deployments, lower storage requirements, and improved performance.

The original Docker image created in the previous exercise is approximately 800 MB in size. In this exercise, your goal is to significantly reduce this size.

Requirements

Starting from the Dockerfile created in the previous exercise, you need to modify it to reduce the image size. The goal is to eliminate unnecessary layers and dependencies while maintaining the functionality of the Dockerized application.

To successfully complete this exercise, you need to:

  • Refactor the Dockerfile to use multi-stage builds, separating the build environment from the runtime environment.
  • Remove any unnecessary files or dependencies to minimize the final image size.
  • Ensure that the final image only contains what is necessary to run the application.

Resources

  • Docker Official Documentation
  • Dockerfile Reference
  • Best Practices for Writing Dockerfiles
  • Project GitHub repository: https://github.com/tdevsin/docker-mastery.git

Acceptance Criteria

  1. Dockerfile Refactored: The Dockerfile must be refactored to use multi-stage builds.
  2. Image Size Reduced: The final Docker image size must be significantly reduced compared to the original Dockerfile. Expected value is less than 300 MB.
  3. Application Runs: The application must run correctly inside the optimized Docker container.
  4. Environment Variables Managed: Ensure the necessary environment variables are correctly passed and handled.

Hints to implement

  • Use multi-stage builds to separate the build dependencies from the runtime environment.
  • Consider using a smaller base image for the final stage, such as eclipse-temurin:21-jre, which includes only the JRE (Java Runtime Environment) instead of the full JDK (Java Development Kit). This will significantly reduce the image size. Research around smaller images.
  • Test the image after each change to ensure the application still runs as expected.

    Mastering Docker

    Unlock All Exercises

  • Getting Started
    • Important - Please Read
  • Introduction to Docker
    • Containerizing a Legacy Application
    • Building Minimal Docker Images
    • Diagnosing a Broken Container
  • Docker Images and Registries
    • Building Multi-Stage Docker Images
    • Pushing Images to a Private Registry
    • Automating Image Builds with Docker Hub
    • Versioning and Tagging Docker Images
    • Securing Docker Images with Trusted Registries
  • Docker Networking
    • Isolating Services with Docker Networks
    • Connecting Microservices Across Networks
    • Debugging Network Issues in Containers
  • Data Persistence
    • Migrating Data with Docker Volumes
    • Data Backup and Recovery in Containers
    • Handling Sensitive Data with Docker Secrets
  • Docker Compose
    • Scaling a Multi-Container Application
    • Handling Environment Variations
  • Docker Security
    • TLS Authentication for Docker Daemon
    • Implementing User Namespaces
    • Monitoring and Responding to Security Threats
    • Securing HTTP APIs with TLS in Containers
  • Advanced Docker Usage
    • Integrating Docker in CI/CD Pipelines
    • Monitoring and Logging with Docker
    • Docker Resource Management
  • Winding Up
    • Final Notes