Cloud Native 13 min read

How to Shrink Docker Images by Up to 98%: Practical Labs with Redis and Go

This tutorial explains Docker image layers, demonstrates step‑by‑step labs that build and progressively slim a Redis image using smaller base images, command chaining, compression tools, and scratch‑based builds, and shows how Go binaries can produce ultra‑small containers.

Open Source Linux
Open Source Linux
Open Source Linux
How to Shrink Docker Images by Up to 98%: Practical Labs with Redis and Go

Introduction

Docker images can be dramatically reduced in size, saving storage and bandwidth. The article uses a Redis example to illustrate common techniques for creating lean Docker images.

Image Layers (Layers)

Each Dockerfile instruction creates a new layer; layers rely on copy‑on‑write, union mounts, and underlying filesystems. Adding or removing files in a layer still contributes to the final image size.

FROM busybox
RUN mkdir /tmp/foo
RUN dd if=/dev/zero of=/tmp/foo/bar bs=1048576 count=100
RUN rm /tmp/foo/bar

Even though the large file is deleted, the layer still records its size, similar to Git history.

Lab Steps

lab‑1: Initialize Redis Image

Basic Dockerfile builds Redis from ubuntu:trusty and installs dependencies.

FROM ubuntu:trusty
ENV VER 3.0.0
ENV TARBALL http://download.redis.io/releases/redis-$VER.tar.gz
RUN apt-get update && apt-get install -y curl make gcc
RUN curl -L $TARBALL | tar zxv
WORKDIR redis-$VER
RUN make && make install
RUN apt-get remove -y --auto-remove curl make gcc && apt-get clean && rm -rf /var/lib/apt/lists/* $VER
CMD ["redis-server"]

Resulting image size: ~106 MB (much larger than the base 1 MB).

lab‑2: Optimize Base Image

Switch to a smaller base such as debian:jessie (≈85 MB) instead of Ubuntu.

FROM debian:jessie
# ... same steps as lab‑1 ...

Image size drops to ~306 MB, a modest improvement.

lab‑3: Chain RUN Commands

Combine multiple RUN statements with && to reduce layer count.

FROM debian:jessie
ENV VER 3.0.0
ENV TARBALL http://download.redis.io/releases/redis-$VER.tar.gz
RUN echo "==> Install curl..." && apt-get update && apt-get install -y curl make gcc \
    && echo "==> Download and compile..." && curl -L $TARBALL | tar zxv && cd redis-$VER && make && make install \
    && echo "==> Clean up..." && apt-get remove -y --auto-remove curl make gcc && apt-get clean && rm -rf /var/lib/apt/lists/* $VER
CMD ["redis-server"]

Image size falls to ~151 MB (≈50% reduction).

lab‑4: Export/Import Compression

Use docker export and docker import to flatten the image, though metadata like ports and env vars are lost.

docker run -d redis:lab-3
docker export <container-id> | docker import - redis:lab-4

Resulting size is similar to the chained version; limited benefit.

lab‑5: Use Minimal Base Images

Build from scratch or busybox to start from an empty filesystem.

FROM scratch
ADD rootfs.tar.gz /
COPY redis.conf /etc/redis/redis.conf
EXPOSE 6379
CMD ["redis-server"]

Final image size can be as low as 7.7 MB when only required shared libraries are added.

lab‑6: Extract Required .so Files

Identify dynamic dependencies with ldd, package them into a tarball, and add to a scratch image.

# ldd redis-3.0.0/src/redis-server
# tar czvf rootfs.tar.gz lib/*.so usr/local/bin/redis-server

This yields a minimal functional image.

lab‑7: Build Tiny Go Images

Go produces static binaries; copying the binary into scratch results in a few‑megabyte image.

package main
import "fmt"
func main() { fmt.Println("Hello World") }
FROM scratch
COPY hello /hello
ENTRYPOINT ["/hello"]

Resulting image size: ~1.6 MB.

Summary

The article covered Docker image layers and presented three main optimization strategies: choosing a smaller base image, chaining Dockerfile commands to reduce layer count, and compressing or rebuilding images with minimal runtimes. Applying these methods can shrink a 300 MB image to as low as 7 MB, achieving reductions of 50%‑98%.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerimage-optimizationredisGoLinuxDockerfile
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.