containerization-and-devops

Class 4 - Multistage Docker Build (Hands-on)

Objective


Environment Used


Experiment Execution with Screenshots

Step 1: Create C Program (hello.c)

C program prints a simple message.

Step 1 -- hello.c


Step 2: Create Single Stage Dockerfile

Dockerfile installs GCC and builds the C program inside Ubuntu image.

Step 2 -- Dockerfile.single


Step 3: Build Single Stage Image

Command executed:

docker build -t hello-single -f Dockerfile.single .

Step 3 -- Build Single Stage


Step 4: Check Docker Images (Single Stage)

Command executed:

docker images

Step 4 -- Image Size Single


Step 5: Run Single Stage Container

Command executed:

docker run hello-single

Step 5 -- Run Single


Step 6: Create Multistage Dockerfile

Second Dockerfile uses builder stage and final scratch stage.

Step 6 -- Dockerfile.multi


Step 7: Build Multistage Image

Command executed:

docker build -t hello-multi -f Dockerfile.multi .

Step 7 -- Build Multi Stage


Step 8: Compare Image Sizes

Command executed:

docker images

Step 8 -- Image Size Comparison


Step 9: Run Multistage Container

Command executed:

docker run hello-multi

Step 9 -- Run Multi


Result

Single-stage Docker image size was significantly larger because it contained Ubuntu base image and GCC compiler.

Multistage Docker build produced a very small image by copying only the compiled binary into a minimal scratch image.


Learning Outcome


- Previous Class | Theory Index | Next Class ->