By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Optimise Docker image size by reducing unnecessary layers and dependencies. Use Alpine base images and squash layers.
FROM python:3.9-slim
docker build -t my-image .
docker images
docker inspect -f '{{.Size}}' my-image
docker run -it my-image
docker stop my-image
docker rm my-image
docker rmi my-image
Purpose: Create a lightweight image with Alpine Linux.Command: docker build -t my-image -f Dockerfile.alpine . Brief explanation of flags: -t sets the image name, -f specifies the Dockerfile.
docker build -t my-image -f Dockerfile.alpine .
-t
-f
Purpose: Reduce the number of layers in the image.Command: docker build -t my-image --squash . Brief explanation of flags: --squash enables layer squashing.
docker build -t my-image --squash .
--squash
Purpose: Remove unnecessary dependencies from the image.Command: docker build -t my-image --no-cache . Brief explanation of flags: --no-cache disables caching, which removes build dependencies.
docker build -t my-image --no-cache .
--no-cache
Error message: Error: unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /path/to/Dockerfile: no such file or directory Why it happens: The Dockerfile is not in the correct location or does not exist.One-line fix: docker build -t my-image .
Error: unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /path/to/Dockerfile: no such file or directory
Error message: Image size remains the same after squashing Why it happens: Squashing only reduces the number of layers, not the actual size.One-line fix: docker build -t my-image --squash --rm .
Image size remains the same after squashing
docker build -t my-image --squash --rm .
Error message: Error: unable to find image 'python:3.9-slim' Why it happens: The base image is not available or not properly configured.One-line fix: docker pull python:3.9-slim
Error: unable to find image 'python:3.9-slim'
docker pull python:3.9-slim
Situation: Image size is over 1GB.Command: docker inspect -f '{{.Size}}' my-image Correct response: Size: 100M
Size: 100M
Situation: Image build fails due to missing dependencies.Command: docker build -t my-image . Correct response: Error: unable to prepare context...
Error: unable to prepare context...
Situation: Layer squashing is not enabled.Command: docker build -t my-image --squash . Correct response: Image size reduced to 50M
Image size reduced to 50M
• Alpine base images: FROM alpine:latest • Squashing layers: docker build -t my-image --squash . • Removing build dependencies: docker build -t my-image --no-cache . • Dockerfile location: docker build -t my-image . • Image size: docker inspect -f '{{.Size}}' my-image • Image build cache: docker build -t my-image --no-cache . • Layer squashing: docker build -t my-image --squash . • Image size reduction: docker build -t my-image --squash --rm . • Missing dependencies: docker build -t my-image . • Image build failure: docker build -t my-image --no-cache . • ⚠️ Dockerfile must be in the same directory as the build command
FROM alpine:latest
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.