0
0
Dockerdevops~5 mins

Image size and minimal base images in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Large Docker images take longer to download and use more storage. Using minimal base images helps keep images small and efficient, making your apps start faster and use fewer resources.
When you want your app to start quickly in a cloud environment with limited bandwidth.
When you need to save disk space on servers running many containers.
When you want to reduce security risks by including fewer unnecessary packages.
When you want faster deployment times during continuous integration and delivery.
When you want to optimize resource usage on development machines.
Config File - Dockerfile
Dockerfile
FROM alpine:3.18

RUN apk add --no-cache python3 py3-pip

COPY app.py /app/app.py

CMD ["python3", "/app/app.py"]

FROM alpine:3.18 sets a very small base image with just essential tools.

RUN apk add --no-cache python3 py3-pip installs Python without caching to keep the image small.

COPY app.py /app/app.py copies your application code into the image.

CMD runs the Python app when the container starts.

Commands
Builds the Docker image named 'my-python-app' from the Dockerfile in the current directory.
Terminal
docker build -t my-python-app .
Expected OutputExpected
Sending build context to Docker daemon 3.072kB Step 1/4 : FROM alpine:3.18 3.18: Pulling from library/alpine Digest: sha256:... Status: Downloaded newer image for alpine:3.18 ---> 3f53bb00af94 Step 2/4 : RUN apk add --no-cache python3 py3-pip ---> Running in abcdef123456 fetch http://dl-cdn.alpinelinux.org/alpine/v3.18/main/x86_64/APKINDEX.tar.gz (1/7) Installing python3 (3.11.4-r0) (2/7) Installing py3-pip (23.1.2-r0) Executing busybox-1.35.0-r21.trigger OK: 15 MiB in 29 packages Removing intermediate container abcdef123456 ---> 123456abcdef Step 3/4 : COPY app.py /app/app.py ---> 789abc123def Step 4/4 : CMD ["python3", "/app/app.py"] ---> Running in 456def789abc Removing intermediate container 456def789abc ---> fedcba987654 Successfully built fedcba987654 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 fedcba987654 10 seconds ago 30MB
Runs the container from the image to check the app works as expected.
Terminal
docker run --rm my-python-app
Expected OutputExpected
Hello from app.py
--rm - Automatically removes the container after it stops to keep the system clean
Key Concept

If you remember nothing else from this pattern, remember: using minimal base images like Alpine keeps your Docker images small and efficient.

Common Mistakes
Using large base images like 'ubuntu' without considering size.
This makes images bulky, slow to download, and wastes storage.
Choose minimal base images like 'alpine' or 'scratch' when possible.
Installing packages without cleaning cache or using no-cache flags.
This leaves unnecessary files in the image, increasing size.
Use package manager flags like '--no-cache' in Alpine to avoid cache files.
Copying unnecessary files into the image.
Extra files increase image size and may expose sensitive data.
Only copy needed files and use .dockerignore to exclude others.
Summary
Use minimal base images like Alpine to reduce Docker image size.
Install only necessary packages with no-cache options to avoid leftover files.
Verify image size with 'docker images' and test the container runs correctly.