Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker in Production Image Size Optimisation Alpine Base Images Squashing Layers Removing Build Dependencies
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-in-production-image-size-optimisation-alpine-base-images-squashing-layers-removing-build-dependencies

Docker Docker Docker in Production Image Size Optimisation Alpine Base Images Squashing Layers Removing Build Dependencies

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Optimise Docker image size by reducing unnecessary layers and dependencies. Use Alpine base images and squash layers.

2. Step-by-Step

  1. Create a new Dockerfile with the following content:
FROM python:3.9-slim
  1. ⚠️ Run docker build -t my-image . to build the image.
  2. Run docker images to list all images, including the newly created one.
  3. Run docker inspect -f '{{.Size}}' my-image to check the size of the image.
  4. Run docker run -it my-image to test the image.
  5. Run docker stop my-image to stop the container.
  6. Run docker rm my-image to remove the container.
  7. Run docker rmi my-image to remove the image.

3. Real Commands with Examples


Example 1: Using Alpine base 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.

Example 2: Squashing layers

Purpose: Reduce the number of layers in the image.
Command: docker build -t my-image --squash . Brief explanation of flags: --squash enables layer squashing.

Example 3: Removing build dependencies

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.

4. Common Errors


Error 1: Image build fails due to missing dependencies

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 2: Image size is not reduced after squashing

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 .

Error 3: Image build fails due to missing base image

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

5. Quick Checks


Scenario 1: Image size is too large

Situation: Image size is over 1GB.
Command: docker inspect -f '{{.Size}}' my-image Correct response: Size: 100M

Scenario 2: Image build fails due to missing dependencies

Situation: Image build fails due to missing dependencies.
Command: docker build -t my-image . Correct response: Error: unable to prepare context...

Scenario 3: Image squashing is not enabled

Situation: Layer squashing is not enabled.
Command: docker build -t my-image --squash . Correct response: Image size reduced to 50M

6. Last-Minute Reference

Alpine base images: FROM alpine:latestSquashing 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-imageImage 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




ADVERTISEMENT