कंटेनर इमेज लेयर: cache, deletion और writable storage समझें
जानें layer में क्या दर्ज होता है, क्रम और build cache कैसे काम करते हैं, फ़ाइल हटाने से image क्यों न घटे और volume कब उपयोग करें।
कंटेनर इमेज लेयर: cache, deletion और writable storage समझें
इमेज लेयर फ़ाइल सिस्टम बदलावों का क्रमबद्ध समूह है। OCI में हर layer नीचे वाली layer के सापेक्ष जोड़े, बदले या हटाए गए फ़ाइलों को दर्ज करती है। Runtime इन्हें क्रम से लागू करके एक filesystem दिखाता है। चलने वाले container में आम तौर पर read-only image layers के ऊपर उसका अपना writable layer जुड़ता है। यह image layer और स्थायी volume से अलग है।
Layer में क्या होता है?
Image manifest configuration और क्रमबद्ध layer blobs का संदर्भ देता है। OCI layers tar आधारित changeset हैं। जोड़ और बदलाव फ़ाइल entries से दर्ज होते हैं; हटाने के लिए whiteout नाम की विशेष entry होती है। क्रम अहम है: ऊपर की layer में बदलाव या हटाना अंतिम view तय करता है।
Environment variables और default command जैसे runtime settings image की configuration में होते हैं, filesystem changes में नहीं। इसलिए Dockerfile की हर पंक्ति वास्तविक filesystem layer नहीं बनाती। Metadata instruction history में रह सकती है लेकिन file diff न बने।
Layer और build cache
जब instruction और input पहले build से मिलते हैं, Docker cached result फिर इस्तेमाल कर सकता है। Dependency manifests को copy और install करने के बाद बार-बार बदलने वाला source copy करें; इससे केवल source बदलने पर installation layer दोबारा काम आ सकती है। पहले के input बदलने से आगे के steps फिर चल सकते हैं।
Cache यह अपने आप नहीं जाँचता कि बाहरी package repository में नए पैकेज हैं या नहीं। Docker के अनुसार सामान्य RUN का cache match मुख्यतः command पर निर्भर है। वही RUN apt-get update पुराना परिणाम दोहरा सकता है। आम तौर पर indexes अपडेट और packages install एक ही RUN में करें, तथा सुरक्षा अपडेट के लिए rebuild policy तय करें। --no-cache build cache छोड़ता है; --pull नया base image अलग से जाँचता है।
फ़ाइल हटाने से image छोटी होती है?
हमेशा नहीं। एक RUN बड़ी फ़ाइल जोड़े और अगला RUN उसे हटाए, तो ऊपर की layer merged filesystem में path छिपा सकती है, पर नीचे की layer का blob image में बना रह सकता है। हटाना नया बदलाव जोड़ता है; पुरानी layer को फिर से नहीं लिखता।
अस्थायी फ़ाइल उसी build step में साफ करें जहाँ बनी, या multi-stage build में अंतिम चरण तक सिर्फ runtime की ज़रूरी फ़ाइलें कॉपी करें। इससे बचा हुआ data कम हो सकता है; जरूरी runtime files, libraries और certificates बने रहने चाहिए।
Writable layer बनाम volume
चलते container में बनाई गई फ़ाइलें आम तौर पर उसी container के writable layer में होती हैं। Container हटने पर data खो सकता है; यह दीर्घकालीन storage नहीं और दूसरे container से अपने आप साझा नहीं होता।
Volume container lifecycle से स्वतंत्र data रखता है। Bind mount host path को container से जोड़ता है। Container बदलने के बाद बचने वाले data के लिए सही volume या bind mount चुनें और ownership, backup, permissions तथा host exposure देखें।
व्यावहारिक नियम और स्रोत
.dockerignore से context छोटा रखें; स्थिर dependency inputs को पहले और बदलते source को बाद में रखें; cache को package freshness न मानें; temporary file उसी step में साफ करें या केवल अंतिम artifacts ले जाएँ; स्थायी data के लिए volume या bind mount चुनें।
OCI layer changeset specification · OCI config, DiffID और ChainID · Docker layers और cache · Cache invalidation · Docker container storage
22 नवंबर 2026 को देखे OCI और Docker दस्तावेज़ों पर आधारित। डिस्क layout और reuse storage driver, image format और builder पर निर्भर है।