📖 Scenario: You want to create a very small Docker container that runs a simple program without any extra files or tools. Using the scratch base image helps you build the smallest possible container.This is useful when you want your container to be lightweight and secure, like sending a small package instead of a big box.
🎯 Goal: Build a Docker image using the scratch base image that runs a simple static binary program.You will create a Dockerfile step-by-step to copy your program into the container and run it.
📋 What You'll Learn
Create a Dockerfile starting from the scratch base image
Copy a static binary file named hello into the container
Set the container to run the hello program when started
Build and run the container to see the output
💡 Why This Matters
🌍 Real World
Using the scratch base image helps create very small and secure containers by including only the necessary files. This is useful for production environments where size and security matter.
💼 Career
Many DevOps and cloud engineering roles require building efficient container images. Knowing how to use scratch images is a valuable skill for optimizing container deployments.
Progress0 / 4 steps
1
Create a Dockerfile with the scratch base image
Create a file named Dockerfile and write the line FROM scratch as the first line to use the minimal base image.
Docker
Hint
The FROM instruction sets the base image. Use scratch for an empty base.
2
Copy the static binary into the container
Add a line to the Dockerfile to copy the file named hello from your current directory into the container's root directory /.
Docker
Hint
Use the COPY instruction to add files into the container.
3
Set the container to run the binary
Add a line to the Dockerfile to set the default command to run the hello program when the container starts. Use the CMD instruction with the executable path /hello.
Docker
Hint
The CMD instruction defines the command to run inside the container.
4
Build and run the Docker container
Build the Docker image with the tag hello-scratch using the command docker build -t hello-scratch .. Then run the container with docker run --rm hello-scratch to see the output.
Docker
Hint
Make sure your hello binary prints Hello from scratch! when run.
Practice
(1/5)
1. What is the main purpose of using the scratch base image in Docker?
easy
A. To automatically install dependencies for your app
B. To include a full Linux operating system inside the container
C. To create a minimal container with only your application and no extra files
D. To provide a graphical user interface for the container
Solution
Step 1: Understand what scratch means
scratch is an empty base image, meaning it has no files or OS inside.
Step 2: Identify the purpose of minimal containers
Using scratch lets you build very small containers by including only your app and its static binaries.
Final Answer:
To create a minimal container with only your application and no extra files -> Option C
Quick Check:
Minimal container = scratch base image [OK]
Hint: Scratch means empty base image for smallest containers [OK]
Believing performance is only affected, not failure
4. You wrote this Dockerfile:
FROM scratch
COPY app /
CMD ["/app"]
But when running the container, it fails with "exec format error". What is the most likely cause?
medium
A. The Dockerfile syntax is incorrect
B. The app file is missing execute permissions
C. The app binary is dynamically linked
D. The app binary is not compiled for the container's CPU architecture
Solution
Step 1: Understand "exec format error" meaning
This error usually means the binary format does not match the CPU architecture.
Step 2: Check other possible causes
Missing execute permission or dynamic linking cause different errors; syntax errors prevent build.
Final Answer:
The app binary is not compiled for the container's CPU architecture -> Option D
Quick Check:
Exec format error = wrong CPU architecture binary [OK]
Hint: Exec format error means binary architecture mismatch [OK]
Common Mistakes:
Confusing exec format error with permission issues
Assuming dynamic linking causes exec format error
Thinking Dockerfile syntax causes this runtime error
5. You want to build a minimal Docker container using scratch that runs a Go application. Which steps should you follow to ensure the container runs correctly?
hard
A. Compile the Go app statically, use FROM scratch, copy the binary, and set CMD
B. Use FROM scratch, copy the Go source code, and run go run inside the container
C. Use FROM alpine, install Go, and compile the app inside the container
D. Compile the Go app dynamically, use FROM scratch, copy the binary, and set CMD
Solution
Step 1: Compile Go app statically
Static compilation bundles all dependencies, so the binary runs without extra libraries.
Step 2: Use FROM scratch and copy binary
Since scratch is empty, copy the static binary and set the command to run it.
Final Answer:
Compile the Go app statically, use FROM scratch, copy the binary, and set CMD -> Option A
Quick Check:
Static binary + scratch base = minimal working container [OK]