Boost Your Docker Images: Proven Tips to Shrink Size and Speed Up Builds
Learn practical Dockerfile optimization techniques—including clean build contexts, selecting minimal base images, configuring domestic package mirrors, setting correct timezones, using virtual build environments, minimizing image layers, and consolidating file additions—to dramatically reduce image size, improve build speed, and avoid common pitfalls.
Building Image Tips
Build Context
When you run
docker build, the current working directory is the build context. Docker sends all files in this directory to the daemon. You can use
-fto specify a Dockerfile, but keep the context clean; avoid creating a Dockerfile and immediately running
docker buildin the same directory.
<code>mkdir project
cd project
vi Dockerfile
# write Dockerfile</code>You can also use a .dockerignore file to exclude unnecessary files from the build context.
Base Image
Prefer small base images such as
alpineor
debian:buster-slim. For Java, use
openjdk:xxx-slim, which is based on a slim Debian image. Note that Alpine uses
musl libcinstead of
glibc, so large projects that depend on
glibc(e.g., OpenJDK, Tomcat, RabbitMQ) may encounter issues and are not recommended on Alpine.
<code>REPOSITORY TAG IMAGE ID CREATED SIZE
debian buster-slim e1af56d072b8 4 days ago 69.2MB
alpine latest cc0abc535e36 8 days ago 5.59MB</code>Domestic Software Sources
Replace default package mirrors with domestic mirrors (Huawei Cloud, Alibaba Cloud, Tencent Cloud, 163.com) for faster downloads. Example commands:
Alpine
<code>echo "http://mirrors.huaweicloud.com/alpine/latest-stable/main/" > /etc/apk/repositories
echo "http://mirrors.huaweicloud.com/alpine/latest-stable/community/" >> /etc/apk/repositories
apk update</code>Debian
<code>sed -i 's/deb.debian.org/mirrors.huaweicloud.com/g' /etc/apt/sources.list
sed -i 's|security.debian.org/debian-security|mirrors.huaweicloud.com/debian-security|g' /etc/apt/sources.list
apt update</code>Ubuntu
<code>sed -i 's/archive.ubuntu.com/mirrors.huaweicloud.com/g' /etc/apt/sources.list
apt update</code>Timezone Settings
Most base images default to UTC, which differs from Beijing time by 8 hours. Set the container timezone with the environment variable
-e TZ=Asia/Shanghaior by installing
tzdataand copying the zoneinfo file.
Alpine
<code>docker run --rm -it -e TZ=Asia/Shanghai alpine date
# or in Dockerfile
RUN apk add --no-cache tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
apk del tzdata</code>Debian
<code>docker run --rm -it -e TZ=Asia/Shanghai debian date
# or copy timezone files in Dockerfile
COPY /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone</code>Ubuntu
<code>docker run --rm -it -e TZ=Asia/Shanghai ubuntu date</code>A handy trick: use the caret ( ^ ) to replace the previous command's image name (e.g., replace debian with ubuntu ).
Use URL to Add Source
When building projects that need source compilation, prefer
gitor
wgetto fetch source inside the image, then delete the source in the same
RUNline to avoid leaving it in a layer.
Original FastDFS Dockerfile (simplified):
<code># centos 7
FROM centos:7
ADD conf/client.conf /etc/fdfs/
ADD conf/http.conf /etc/fdfs/
ADD conf/mime.types /etc/fdfs/
ADD conf/storage.conf /etc/fdfs/
ADD conf/tracker.conf /etc/fdfs/
ADD fastdfs.sh /home
ADD conf/nginx.conf /etc/fdfs/
ADD conf/mod_fastdfs.conf /etc/fdfs
ADD source/libfastcommon.tar.gz /usr/local/src/
ADD source/fastdfs.tar.gz /usr/local/src/
ADD source/fastdfs-nginx-module.tar.gz /usr/local/src/
ADD source/nginx-1.15.4.tar.gz /usr/local/src/
RUN yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y && \
mkdir /home/dfs && \
cd /usr/local/src && \
... (build steps) ...
EXPOSE 22122 23000 8888 80
ENTRYPOINT ["/home/fastdfs.sh"]</code>Optimized Dockerfile:
<code>FROM alpine:3.10
RUN set -x \
&& echo "http://mirrors.huaweicloud.com/alpine/latest-stable/main/" > /etc/apk/repositories \
&& echo "http://mirrors.huaweicloud.com/alpine/latest-stable/community/" >> /etc/apk/repositories \
&& apk update \
&& apk add --no-cache --virtual .build-deps gcc libc-dev make perl-dev openssl-dev pcre-dev zlib-dev git \
&& mkdir -p /usr/local/src \
&& cd /usr/local/src \
&& git clone https://github.com/happyfish100/libfastcommon.git --depth 1 \
&& git clone https://github.com/happyfish100/fastdfs.git --depth 1 \
&& git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1 \
&& wget http://nginx.org/download/nginx-1.15.4.tar.gz \
&& tar -xf nginx-1.15.4.tar.gz \
&& cd libfastcommon && ./make.sh && ./make.sh install \
&& cd ../fastdfs && ./make.sh && ./make.sh install \
&& cd ../nginx-1.15.4 && ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/ && make && make install \
&& apk del .build-deps && apk add --no-cache pcre-dev bash \
&& mv /usr/local/src/fastdfs/docker/dockerfile_network/fastdfs.sh /home \
&& mv /usr/local/src/fastdfs/docker/dockerfile_network/conf/* /etc/fdfs \
&& chmod +x /home/fastdfs.sh \
&& rm -rf /usr/local/src*
VOLUME /home/dfs
EXPOSE 22122 23000 8888 8080
CMD ["/home/fastdfs.sh"]</code>Result: the original image size (~500 MB) is reduced to ~30 MB after optimization.
Minimize Layers
Since Docker 1.10 only
RUN,
COPY, and
ADDcreate layers. Combine multiple commands with
&&or
;into a single
RUNto keep the layer count low. For many files, pack them into a single tarball and extract in one step:
<code>FROM alpine:3.10
COPY src.tar.gz /usr/local/src.tar.gz
RUN set -xe \
&& apk add --no-cache --virtual .build-deps gcc libc-dev make perl-dev openssl-dev pcre-dev zlib-dev tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& tar -xvf /usr/local/src.tar.gz -C /usr/local \
&& mv /usr/local/src/conf/fastdfs.sh /home/fastdfs/ \
&& mv /usr/local/src/conf/* /etc/fdfs \
&& chmod +x /home/fastdfs/fastdfs.sh \
&& rm -rf /usr/local/src/* /var/cache/apk/* /tmp/* $HOME/.cache
VOLUME /var/fdfs</code>Other layer‑reduction tricks include chaining commands with
&&or
;; the latter is often seen in official Dockerfiles.
Source: https://blog.k8s.li/dockerfile-tips.html (article slightly trimmed).
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.