0
0
Dockerdevops~30 mins

Builder pattern before multi-stage in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Builder Pattern Before Multi-Stage Dockerfiles
📖 Scenario: You are working on a Docker project where you want to build a small, efficient image. Before Docker had multi-stage builds, developers used a builder pattern to create the final image in two steps.This project will guide you to create a Dockerfile that uses the builder pattern manually by creating two separate Dockerfiles: one for building the app and one for the final runtime image.
🎯 Goal: Build a Docker setup that first compiles a simple app in a builder image, then copies the compiled app into a smaller runtime image manually, simulating the builder pattern before multi-stage builds.
📋 What You'll Learn
Create a Dockerfile named Dockerfile.builder that builds the app
Create a Dockerfile named Dockerfile.runtime that copies the built app from the builder image
Use exact image names and commands as specified
Show the final image running the app prints the expected output
💡 Why This Matters
🌍 Real World
Before Docker supported multi-stage builds, developers used this pattern to keep final images small by separating build and runtime environments.
💼 Career
Understanding this pattern helps you maintain legacy Docker projects and appreciate modern multi-stage builds in DevOps workflows.
Progress0 / 4 steps
1
Create the builder Dockerfile
Create a file named Dockerfile.builder with these exact contents:
FROM python:3.12-slim
WORKDIR /app
COPY app.py /app/
RUN python -m compileall app.py

This will prepare a builder image that compiles app.py.
Docker
Need a hint?

Use the official Python 3.12 slim image as the base.

Set the working directory to /app.

Copy app.py into the image.

Run python -m compileall app.py to compile the script.

2
Create the runtime Dockerfile
Create a file named Dockerfile.runtime with these exact contents:
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /app/__pycache__/app.cpython-312.pyc /app/app.pyc
CMD ["python", "/app/app.pyc"]

This Dockerfile will copy the compiled app from the builder image and run it.
Docker
Need a hint?

Use the same base image and working directory.

Copy the compiled .pyc file from the builder image's /app/__pycache__ folder.

Set the command to run the compiled Python file.

3
Build the builder image
Run this exact command in your terminal to build the builder image:
docker build -f Dockerfile.builder -t builder .
This creates an image named builder that compiles the app.
Docker
Need a hint?

Use docker build with -f Dockerfile.builder and tag the image as builder.

4
Build and run the runtime image
Run these exact commands in your terminal:
docker build -f Dockerfile.runtime -t runtime .
Then run:
docker run --rm runtime
The output should be:
Hello from compiled app!
Docker
Need a hint?

Build the runtime image using Dockerfile.runtime and tag it runtime.

Run the container and check the output.