आप अक्सर git clone
डॉक बिल्ड के भीतर से एक निजी रेपो प्रदर्शन नहीं करना चाहते हैं । वहाँ क्लोन करने से छवि के अंदर निजी ssh क्रेडेंशियल्स रखना शामिल होता है जहाँ उन्हें बाद में आपकी छवि के साथ किसी के द्वारा भी निकाला जा सकता है।
इसके बजाय, सामान्य अभ्यास अपनी पसंद के सीआई उपकरण में डॉक के बाहर से गिट रेपो को क्लोन करना है, और बस COPY
फाइलों को छवि में। इसका दूसरा लाभ है: डॉकटर कैशिंग। डॉकर कैचिंग को चलाए जा रहे कमांड को देखता है, इसमें पर्यावरण चर शामिल हैं, इनपुट फाइलें, आदि, और यदि वे एक ही मूल कदम से पिछले बिल्ड के समान हैं, तो यह उस पिछले कैश का पुन: उपयोग करता है। एक git clone
कमांड के साथ , कमांड स्वयं समान है, इसलिए डॉकटर कैश को फिर से उपयोग करेगा भले ही बाहरी गिट रेपो को बदल दिया जाए। हालांकि, एक COPY
कमांड बिल्ड संदर्भ में फाइलों को देखेगा और यह देख सकता है कि क्या वे समान हैं या अपडेट किए गए हैं, और उपयुक्त होने पर ही कैश का उपयोग करें।
यदि आप अपने बिल्ड में क्रेडेंशियल्स जोड़ने जा रहे हैं, तो एक मल्टी-स्टेज बिल्ड के साथ ऐसा करने पर विचार करें, और केवल उन क्रेडेंशियल्स को एक प्रारंभिक चरण में रखें जो कभी भी आपके बिल्ड होस्ट के बाहर टैग और पुश न किए गए हों। परिणाम जैसा दिखता है:
FROM ubuntu as clone
# Update aptitude with new repo
RUN apt-get update \
&& apt-get install -y git
# Make ssh dir
# Create known_hosts
# Add bitbuckets key
RUN mkdir /root/.ssh/ \
&& touch /root/.ssh/known_hosts \
&& ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
# Copy over private key, and set permissions
# Warning! Anyone who gets their hands on this image will be able
# to retrieve this private key file from the corresponding image layer
COPY id_rsa /root/.ssh/id_rsa
# Clone the conf files into the docker container
RUN git clone git@bitbucket.org:User/repo.git
FROM ubuntu as release
LABEL maintainer="Luke Crooks <luke@pumalo.org>"
COPY --from=clone /repo /repo
...
अभी हाल ही में, BuildKit कुछ प्रायोगिक विशेषताओं का परीक्षण कर रहा है जो आपको एक ssh कुंजी को माउंट के रूप में पारित करने की अनुमति देते हैं जो कभी भी छवि को नहीं लिखा जाता है:
# syntax=docker/dockerfile:experimental
FROM ubuntu as clone
LABEL maintainer="Luke Crooks <luke@pumalo.org>"
# Update aptitude with new repo
RUN apt-get update \
&& apt-get install -y git
# Make ssh dir
# Create known_hosts
# Add bitbuckets key
RUN mkdir /root/.ssh/ \
&& touch /root/.ssh/known_hosts \
&& ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
# Clone the conf files into the docker container
RUN --mount=type=secret,id=ssh_id,target=/root/.ssh/id_rsa \
git clone git@bitbucket.org:User/repo.git
और आप इसके साथ निर्माण कर सकते हैं:
$ DOCKER_BUILDKIT=1 docker build -t your_image_name \
--secret id=ssh_id,src=$(pwd)/id_rsa .
ध्यान दें कि इसके लिए अभी भी आपकी ssh कुंजी की आवश्यकता है कि पासवर्ड सुरक्षित न हो, लेकिन आप कम से कम एक चरण में निर्माण को चला सकते हैं, एक COPY कमांड को हटा सकते हैं, और किसी छवि का हिस्सा होने से ssh क्रेडेंशियल से बच सकते हैं।
बिल्डकिट ने सिर्फ ssh के लिए एक फीचर जोड़ा है जो आपको अभी भी आपके पासवर्ड को सुरक्षित ssh कीज़ की अनुमति देता है, परिणाम इस तरह दिखता है:
# syntax=docker/dockerfile:experimental
FROM ubuntu as clone
LABEL maintainer="Luke Crooks <luke@pumalo.org>"
# Update aptitude with new repo
RUN apt-get update \
&& apt-get install -y git
# Make ssh dir
# Create known_hosts
# Add bitbuckets key
RUN mkdir /root/.ssh/ \
&& touch /root/.ssh/known_hosts \
&& ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
# Clone the conf files into the docker container
RUN --mount=type=ssh \
git clone git@bitbucket.org:User/repo.git
और आप इसके साथ निर्माण कर सकते हैं:
$ eval $(ssh-agent)
$ ssh-add ~/.ssh/id_rsa
(Input your passphrase here)
$ DOCKER_BUILDKIT=1 docker build -t your_image_name \
--ssh default=$SSH_AUTH_SOCK .
फिर, इसे बिल्ड में बिना किसी इमेज लेयर के लिखे जाने पर इंजेक्ट किया जाता है, जिससे यह जोखिम दूर हो जाता है कि क्रेडेंशियल गलती से लीक हो सकता है।
डॉकटर को बाध्य करने के लिए मजबूर करने के लिए git clone
जब भी लाइनों को कैश किया गया हो, तो आप एक बिल्ड एआरजी को इंजेक्ट कर सकते हैं जो कैश को तोड़ने के लिए प्रत्येक बिल्ड के साथ बदलता है। जो दिखता है:
# inject a datestamp arg which is treated as an environment variable and
# will break the cache for the next RUN command
ARG DATE_STAMP
# Clone the conf files into the docker container
RUN git clone git@bitbucket.org:User/repo.git
तब आप उस बदलते बिल्ड कमांड में arg को इंजेक्ट करते हैं:
date_stamp=$(date +%Y%m%d-%H%M%S)
docker build --build-arg DATE_STAMP=$date_stamp .