Best Practices for Writing Efficient Dockerfiles
This article explains the advantages of using Dockerfiles for image building, outlines key principles such as single responsibility, commenting, minimalism, proper base image selection, .dockerignore usage, cache optimization, and timezone configuration, and provides a practical example Dockerfile to improve build speed and image size.
Using a Dockerfile to build images offers many benefits, including easy version control, clear build process traceability, and consistent results across environments, but improper use can lead to long build times, failures, and oversized images; this lesson teaches how to write optimal Dockerfiles for production.
Dockerfile Writing Principles
(1) Single Responsibility : Each container should run a single process, so different functionalities are split into separate containers.
(2) Provide Comments : Treat the Dockerfile as code, add comments to make each instruction’s purpose clear for collaborators.
(3) Keep Containers Minimal : Avoid installing unnecessary packages (e.g., vim, gcc) to speed up builds and reduce image size.
(4) Choose an Appropriate Base Image : Use the smallest base that satisfies the application’s runtime requirements (e.g., JRE instead of JDK for Java apps).
(5) Use a .dockerignore File : Similar to .gitignore, it excludes files that are not needed for the build, improving efficiency.
(6) Leverage Build Cache : Docker creates a layer for each instruction; placing rarely‑changed instructions early increases cache hit probability, while frequently‑changed steps (e.g., copying source code) should be placed later.
(7) Set the Correct Timezone : Adjust the container’s timezone to match the desired region (e.g., China Standard Time).
.dockerignore Syntax Examples
Lines starting with # are comments and are ignored. Patterns such as /tmp, *.md, and tem? match files or directories, while a leading ! excludes a previously ignored pattern (e.g., !README.md).
Example .dockerignore content:
*.md !README.mdThese rules ignore all markdown files except README.md.
Cache Determination Rules
Docker compares each instruction and its context (including checksums for ADD and COPY) with previous builds; if they match, the cache is used. Therefore, place stable commands like package installation early, and mutable commands like source compilation later.
Example Dockerfile Ordering
FROM centos:7 # Set environment variables early ENV PATH /usr/local/bin:$PATH # Install required packages early RUN yum install -y make # Place frequently changing application steps at the end ...Following these principles increases the likelihood of cache hits, significantly reducing image build time.
Timezone Configuration
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN echo "Asia/Shanghai" >> /etc/timezoneThese commands set the container’s timezone to China Standard Time.
--- End of article ---
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
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.
