0
0
Dockerdevops~5 mins

Reducing image size strategies in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Large Docker images take longer to download and use more storage. Reducing image size makes your apps start faster and saves space on servers.
When you want your app to start quickly on any machine or cloud.
When you have limited storage space on your server or local machine.
When you want to reduce data transfer time during deployments.
When you want to keep your Docker images clean and efficient.
When you want to avoid including unnecessary files or tools in your app image.
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"]

This Dockerfile uses a slim base image to keep size small.

It installs Python packages without cache to avoid leftover files.

It copies only necessary files and sets the command to run the app.

Commands
Builds the Docker image using the Dockerfile in the current folder and tags it as 'my-app:small'.
Terminal
docker build -t my-app:small .
Expected OutputExpected
Sending build context to Docker daemon 12.3MB Step 1/6 : FROM python:3.11-slim ---> 123abc456def Step 2/6 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/6 : COPY requirements.txt . ---> Using cache ---> 456abc789def Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 1a2b3c4d5e6f Collecting flask Downloading Flask-2.3.2-py3-none-any.whl (96 kB) Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container 1a2b3c4d5e6f ---> 0f1e2d3c4b5a Step 5/6 : COPY . . ---> 9a8b7c6d5e4f Step 6/6 : CMD ["python", "app.py"] ---> Running in 7f8e9d0c1b2a Removing intermediate container 7f8e9d0c1b2a ---> 3c2b1a0f9e8d Successfully built 3c2b1a0f9e8d Successfully tagged my-app:small
Shows the size of the newly built image to verify it is small.
Terminal
docker images my-app:small
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-app small 3c2b1a0f9e8d 10 seconds ago 45MB
Runs the small image to check the app works correctly.
Terminal
docker run --rm my-app:small
Expected OutputExpected
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
--rm - Automatically removes the container after it stops to save space.
Key Concept

If you remember nothing else from this pattern, remember: use slim base images and clean up caches to keep Docker images small and efficient.

Common Mistakes
Using a full base image like 'python:3.11' instead of 'python:3.11-slim'.
Full images include many extra tools and files, making the image much larger than needed.
Choose slim or minimal base images that have only what your app needs.
Not using '--no-cache-dir' when installing Python packages.
Pip caches packages by default, which increases image size unnecessarily.
Add '--no-cache-dir' to pip install commands to avoid saving cache files.
Copying the entire project including unnecessary files like tests or docs.
Unneeded files increase image size and slow down builds and deployments.
Use a .dockerignore file or carefully copy only required files.
Summary
Use slim base images to reduce unnecessary files in your Docker image.
Install packages without cache to avoid leftover files increasing image size.
Copy only needed files to keep the image clean and small.
Verify image size with 'docker images' and test the image by running it.