Objective: Create an optimized Docker image for a Java application using multi-stage builds.
Instructions:
-
Create a simple Spring Boot application or use an existing one
-
Write a Dockerfile with two stages:
- Stage 1: Use
maven:3.8-openjdk-17
as the build image - Stage 2: Use
openjdk:11-ea-17-jre-slim
as the production image
- Stage 1: Use
-
In the first stage, copy your source code, run Maven to build the JAR
-
In the second stage, copy only the built JAR from the first stage
-
Configure the proper entrypoint to run the application
-
Build and run your container
-
Compare the image size with a single-stage build
Success Criteria:
- The application runs correctly in the container
- The multi-stage image is significantly smaller than a single-stage version
- No build tools or source code exists in the final image
Objective: Create a multi-stage build for a full-stack application with separate frontend and backend components.
Instructions:
-
Use a simple application with a Node.js/React frontend and a Python/Flask backend
-
Create a Dockerfile with three stages:
- Stage 1: Build the frontend using
node:16
as base - Stage 2: Build the backend using
python:3.9
as base - Stage 3: Create a production image using
nginx:alpine
for serving both
- Stage 1: Build the frontend using
-
In the first stage, install dependencies and build the frontend
-
In the second stage, install Python dependencies
-
In the final stage, copy the built frontend assets to serve statically
-
Copy the backend code and configure nginx to proxy API requests
-
Build and run the container
Success Criteria:
- The application works end-to-end with proper communication between frontend and backend
- The final image is optimized for production with minimal size
- Build tools and source code only exist in their respective build stages
Objective: Build and publish container images that run on multiple CPU architectures.
Instructions:
- Create a simple application (e.g., a Python web server)
- Set up Docker Buildx with proper emulation support
- Create a Dockerfile compatible with multiple architectures
- Build for ARM64, AMD64, and ARM/v7 platforms in a single command
- Test your image on different platforms (using emulation if needed)
- Push to a container registry with proper platform manifests
Success Criteria:
- Successfully build one image for multiple platforms
- Verify the image can run on different architectures
- Properly tag and publish the multi-platform image
- Demonstrate pulling the correct architecture automatically
Objective: Create a complete development environment for a microservices application using Docker Compose.
Instructions:
-
Design a system with at least 3 services:
- A web frontend
- An API service
- A database
-
Write individual Dockerfiles for each custom service
-
Create a
docker-compose.yml
file that:- Defines all services with proper dependencies
- Sets up a custom network
- Configures persistent volumes for the database
- Includes environment variables for configuration
- Implements healthchecks for critical services
-
Add Docker Compose overrides for development vs. production environments
-
Implement proper logging configuration
Success Criteria:
- All services start and communicate properly
- Data persists between container restarts
- Development mode enables features like hot reloading
- Proper service discovery works between containers
- The application can be started with a single command
Each of these exercises builds upon core Docker concepts while introducing more advanced practices that are essential in professional DevOps environments. The progression helps learners understand how these technologies fit together in real-world scenarios.