📖 Scenario: You are working on a Docker project where keeping the image size small is important for faster downloads and efficient storage. You will create a Dockerfile using a minimal base image and add a simple file to it.
🎯 Goal: Build a Docker image using a minimal base image alpine and add a text file hello.txt with the content Hello, Docker!. Then display the image size using Docker commands.
📋 What You'll Learn
Create a Dockerfile starting with the alpine base image
Add a file named hello.txt with the exact content Hello, Docker!
Use a command to display the size of the built Docker image
💡 Why This Matters
🌍 Real World
Keeping Docker images small helps in faster deployment, less storage use, and quicker updates in real projects.
💼 Career
Understanding minimal base images and image size is essential for DevOps engineers to optimize containerized applications.
Progress0 / 4 steps
1
Create a minimal Dockerfile with alpine base
Create a file named Dockerfile with the first line exactly FROM alpine to use the minimal Alpine Linux base image.
Docker
Hint
The first line in a Dockerfile specifies the base image. Alpine is a very small Linux image.
2
Add a text file with content
Add these two lines to the Dockerfile: RUN echo "Hello, Docker!" > /hello.txt to create the file inside the image.
Docker
Hint
Use the RUN command to execute shell commands inside the image during build.
3
Build the Docker image with a tag
Write the exact command to build the Docker image with tag hello-alpine using the current directory as context: docker build -t hello-alpine .
Docker
Hint
The docker build command creates the image from the Dockerfile.
4
Display the size of the built image
Write the exact command to show the size of the image named hello-alpine: docker images hello-alpine --format "{{.Repository}}: {{.Size}}"
Docker
Hint
This command lists images filtered by name and shows their size in a simple format.
Practice
(1/5)
1. Which base image is known for being very small and commonly used to create minimal Docker images?
easy
A. Ubuntu
B. Alpine
C. Debian
D. CentOS
Solution
Step 1: Understand common base images
Ubuntu, Debian, and CentOS are full Linux distributions and tend to be larger in size.
Step 2: Identify minimal base images
Alpine is a lightweight Linux distribution designed for minimal size, often under 10 MB.
Final Answer:
Alpine -> Option B
Quick Check:
Minimal base image = Alpine [OK]
Hint: Alpine is the go-to small base image for minimal Docker images [OK]
Common Mistakes:
Choosing Ubuntu or Debian as minimal base images
Confusing full distributions with minimal ones
Not knowing Alpine is designed for small size
2. Which of the following Dockerfile base image declarations is syntactically correct for using Alpine Linux?
easy
A. FROM alpine:latest
B. BASE alpine
C. USE alpine
D. IMAGE alpine
Solution
Step 1: Recall Dockerfile syntax for base images
The correct keyword to specify a base image is FROM followed by the image name and optional tag.
Step 2: Check each option
Only 'FROM alpine:latest' uses the correct syntax. Others are invalid Dockerfile commands.
Final Answer:
FROM alpine:latest -> Option A
Quick Check:
Base image syntax = FROM [OK]
Hint: Base images start with FROM in Dockerfiles [OK]
Common Mistakes:
Using incorrect keywords like BASE or USE
Omitting the FROM keyword
Not specifying image tag correctly
3. Given this Dockerfile snippet:
FROM alpine:3.18
RUN apk add --no-cache curl
What is the main reason this image remains small compared to using Ubuntu as base?
medium
A. Alpine automatically removes all packages after installation
B. Alpine installs curl faster than Ubuntu
C. Ubuntu images do not support curl installation
D. Alpine uses apk with --no-cache to avoid cache files, reducing size