0
0
Dockerdevops~5 mins

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

Choose your learning style9 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.