0
0
Dockerdevops~5 mins

Reducing final image size by 80 percent in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Large Docker images take longer to download and use more storage. Reducing the image size by 80 percent helps apps start faster and saves space on servers.
When you want your app to start quickly on any machine.
When you have limited storage on your server or cloud.
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 Python base image to keep the size small.

It installs only required packages without caching to avoid extra files.

It copies only necessary files into the image.

The CMD runs the Python app.

Commands
Builds the Docker image using the Dockerfile that reduces size by using a slim base and no cache during install.
Terminal
docker build -t my-app:small .
Expected OutputExpected
Sending build context to Docker daemon 5.12MB Step 1/5 : FROM python:3.11-slim ---> 123abc456def Step 2/5 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/5 : COPY requirements.txt . ---> Using cache ---> 345abc678def Step 4/5 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in 456def789abc Collecting flask Installing collected packages: flask Successfully installed flask-2.3.2 Removing intermediate container 456def789abc ---> 901abc234def Step 5/5 : COPY . . ---> 567def890abc Successfully built 567def890abc Successfully tagged my-app:small
-t - Names the image for easy reference
Shows the size of the newly built small image to verify the reduction.
Terminal
docker images my-app:small
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE my-app small 567def890abc 10 seconds ago 45MB
Runs the small image to confirm the app works correctly after size reduction.
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
Key Concept

If you remember nothing else from this pattern, remember: use slim base images and avoid caching to drastically reduce Docker image size.

Common Mistakes
Using a full base image like python:3.11 instead of python:3.11-slim
Full base images include unnecessary tools and files, making the image much larger.
Use slim or alpine base images that contain only essential components.
Not using --no-cache-dir when installing packages
Pip caches installation files inside the image, increasing size unnecessarily.
Add --no-cache-dir flag to pip install commands to avoid caching.
Copying the entire project including unnecessary files like docs or tests
Unneeded files increase image size and slow down builds.
Use .dockerignore to exclude unnecessary files or copy only needed files explicitly.
Summary
Use a slim base image to start with a smaller foundation.
Install packages without cache to avoid extra files in the image.
Copy only necessary files to keep the image clean and small.
Verify image size with docker images command.
Run the image to ensure the app still works after size reduction.