Bird
Raised Fist0
Dockerdevops~5 mins

Choosing small base images (alpine, slim) in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Large container images take longer to download and use more disk space. Choosing small base images like alpine or slim helps make your containers lightweight and faster to start.
When you want your application containers to start quickly on any machine.
When you have limited disk space on your server or developer machine.
When you want to reduce network bandwidth usage during deployments.
When you want to improve security by minimizing the software inside your container.
When you want to speed up your continuous integration and delivery pipelines.
Config File - Dockerfile
Dockerfile
FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]

FROM python:3.11-slim uses a smaller Python base image with fewer extra tools to keep the image size small.

WORKDIR /app sets the working directory inside the container.

COPY requirements.txt . copies the dependency list.

RUN pip install --no-cache-dir -r requirements.txt installs dependencies without caching to reduce image size.

COPY . . copies the application code.

CMD runs the app.

Commands
Builds the Docker image named 'my-python-app' using the Dockerfile in the current directory.
Terminal
docker build -t my-python-app .
Expected OutputExpected
Sending build context to Docker daemon 12.34MB Step 1/6 : FROM python:3.11-slim 3.11-slim: Pulling from library/python Digest: sha256:... Status: Downloaded newer image for python:3.11-slim ---> abcdef123456 Step 2/6 : WORKDIR /app ---> Running in 123abc456def Removing intermediate container 123abc456def ---> 789def012345 Step 3/6 : COPY requirements.txt . ---> 456abc789def Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 789abc123def Collecting flask Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container 789abc123def ---> 012def345abc Step 5/6 : COPY . . ---> 345abc678def Step 6/6 : CMD ["python", "app.py"] ---> Running in def123abc456 Removing intermediate container def123abc456 ---> 678def901abc Successfully built 678def901abc Successfully tagged my-python-app:latest
Shows the size and details of the built image to verify it is small.
Terminal
docker images my-python-app
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-python-app latest 678def901abc 10 seconds ago 120MB
Runs the container to check the Python version and confirm the image works as expected.
Terminal
docker run --rm my-python-app python --version
Expected OutputExpected
Python 3.11.4
→
--rm - Automatically removes the container after it exits to keep your system clean.
Key Concept

If you remember nothing else from this pattern, remember: using small base images like alpine or slim makes your containers faster and lighter.

Common Mistakes
Using the full base image like 'python:3.11' instead of 'python:3.11-slim' or 'alpine'.
Full images include many extra tools and libraries, making the container large and slow to download.
Choose 'slim' or 'alpine' tags for base images to keep your container size small.
Not using '--no-cache-dir' when installing packages inside the container.
Pip caches installation files which increase the image size unnecessarily.
Always use 'pip install --no-cache-dir' to avoid caching and reduce image size.
Copying unnecessary files into the container increasing image size.
Extra files add to the image size and can slow down builds and deployments.
Use a .dockerignore file to exclude files not needed in the container.
Summary
Use small base images like alpine or slim to reduce container size and speed up startup.
Build your Docker image and check its size with 'docker images' to confirm it is small.
Run your container with '--rm' to test it and keep your system clean.

Practice

(1/5)
1. Why do developers often choose small base images like alpine or slim when building Docker containers?
easy
A. To reduce image size and speed up container startup
B. To increase the number of installed packages by default
C. To make the container run only on Windows systems
D. To disable networking inside the container

Solution

  1. Step 1: Understand the purpose of small base images

    Small base images like alpine or slim are designed to be lightweight, which means they have fewer files and smaller size.
  2. Step 2: Connect size to performance

    Smaller images download faster and start containers quicker, improving efficiency.
  3. Final Answer:

    To reduce image size and speed up container startup -> Option A
  4. Quick Check:

    Small base images = smaller size and faster startup [OK]
Hint: Small images mean faster downloads and startups [OK]
Common Mistakes:
  • Thinking small images have more packages by default
  • Assuming small images restrict OS compatibility
  • Believing small images disable networking
2. Which of the following is the correct way to specify the Alpine base image in a Dockerfile?
easy
A. IMAGE alpine:latest
B. FROM alpine/latest
C. BASE alpine:latest
D. FROM alpine:latest

Solution

  1. Step 1: Recall Dockerfile syntax for base images

    The correct keyword to specify a base image is FROM, followed by the image name and tag separated by a colon.
  2. Step 2: Check each option

    FROM alpine:latest uses FROM alpine:latest, which is correct syntax. Options A, B, and C use incorrect keywords or separators.
  3. Final Answer:

    FROM alpine:latest -> Option D
  4. Quick Check:

    Base image syntax = FROM image:tag [OK]
Hint: Use FROM keyword with colon-separated tag [OK]
Common Mistakes:
  • Using slash instead of colon between image and tag
  • Using BASE or IMAGE instead of FROM
  • Omitting the tag separator
3. Given this Dockerfile snippet:
FROM python:3.12-alpine
RUN python --version

What will be the expected output when building this image?
medium
A. Python 3.12.x (Alpine variant)
B. Error: python command not found
C. Python 2.7.x default version
D. No output, build fails silently

Solution

  1. Step 1: Identify the base image and its contents

    The base image is python:3.12-alpine, which includes Python 3.12 installed on Alpine Linux.
  2. Step 2: Understand the RUN command effect

    The command python --version will print the Python version installed in the image during build.
  3. Final Answer:

    Python 3.12.x (Alpine variant) -> Option A
  4. Quick Check:

    python:3.12-alpine includes Python 3.12 [OK]
Hint: Tagged python alpine images include Python by default [OK]
Common Mistakes:
  • Assuming python is missing in alpine variant
  • Confusing Python 2 with Python 3
  • Expecting silent build without output
4. You wrote this Dockerfile:
FROM node:18-slim
RUN apk add curl

But the build fails with "apk: command not found". What is the problem?
medium
A. You must install curl before FROM statement
B. Node 18-slim image does not support RUN commands
C. The slim image uses apt, not apk, so apk command is missing
D. The command should be RUN apt-get add curl

Solution

  1. Step 1: Identify the base image OS

    The node:18-slim image is based on Debian, which uses apt package manager, not apk.
  2. Step 2: Understand the error cause

    Using apk (Alpine package manager) on Debian-based image causes "command not found" error.
  3. Final Answer:

    The slim image uses apt, not apk, so apk command is missing -> Option C
  4. Quick Check:

    Debian slim uses apt, not apk [OK]
Hint: Use apt on slim (Debian), apk on alpine [OK]
Common Mistakes:
  • Trying apk on Debian-based images
  • Placing RUN commands before FROM
  • Using unsupported RUN syntax
5. You want a small Docker image for a Python app but need some common Linux tools like curl and bash. Which base image choice balances size and compatibility best?
hard
A. python:3.12-alpine (smallest but may need extra setup)
B. python:3.12-slim (slightly larger but includes common tools)
C. python:3.12-full (largest, includes all tools by default)
D. python:3.12-minimal (no tools, smallest size)

Solution

  1. Step 1: Compare alpine and slim images

    Alpine is smallest but often lacks common tools, requiring manual installs. Slim is bigger but includes many common Linux utilities.
  2. Step 2: Match needs to image choice

    Since you need tools like curl and bash, slim is a better balance of size and compatibility than alpine.
  3. Final Answer:

    python:3.12-slim (slightly larger but includes common tools) -> Option B
  4. Quick Check:

    Slim balances size and tools better than alpine [OK]
Hint: Choose slim for tools, alpine for smallest size [OK]
Common Mistakes:
  • Picking alpine without adding needed tools
  • Assuming minimal includes tools
  • Choosing full image without size concern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard