如何在Docker中忽略檔案?
介紹
在這篇文章中,我們將學習Docker中的忽略檔案。如何建立忽略檔案以及如何使用各種方法將檔案和資料夾新增到忽略檔案中。所有先決條件知識都以逐步的方式列出。在瞭解程式碼行業中忽略檔案的要求之前,請不要直接跳到實現部分。
什麼是忽略檔案?
忽略檔案是在不同的系統中建立的,例如Git倉庫和Dockerfile。這些檔案有助於排除某些檔案和目錄不被用於或複製到輸出中。Dockerfile的輸出是映象,對於Git,它可能是提交。DevOps中一些最常用的忽略檔案示例是“gitignore”和“dockerignore”。
要忽略的檔案型別
主要任務是忽略某些內容。這些內容可以是不同型別的。例如,它可能是Python中的程式檔案或帶有子目錄的ReactJS應用程式目錄。所有這些檔案都列在下面的列表中。
目錄
檔案 - 文字或程式檔案
子目錄
.dockerignore本身
忽略這些檔案的必要性是因為我們不再需要這些檔案在最終產品中。這些檔案只需要在主機上。
方法
在本節中,我們將學習如何透過將不同型別的檔案新增到dockerignore檔案中來排除它們。
所有將要出現在dockerignore中的檔案都應相對於Docker上下文目錄進行說明。Docker上下文是包含需要新增到映象中所有有用檔案的目錄路徑。在構建映象時,我們通常使用.(點)作為Docker上下文。
建立“.dockerignore”檔案
“.dockerignore”應該在Dockerfile所在的目錄中建立,否則它將無法工作。使用以下命令建立和排除各種型別的檔案。
$touch .dockerignore
我們建立了一個目錄結構來解釋所有這些方法。
$tree -la
輸出
. ├── dir1 ├── dir2 ├── dir3 │ ├── dir3_1 │ │ └── file_dir3_1.txt │ ├── dir3_2 │ ├── file1_dir3.txt │ └── file2_dir3.py ├── Dockerfile ├── .dockerignore ├── file1.txt ├── file2.sh ├── file3.py ├── no_ex1 └── No_ex2 5 directories, 10 files
開啟忽略檔案以從上面的目錄結構中新增各種檔案。
$nano .dockerignore
在docker忽略檔案中新增不同型別的檔案、目錄和註釋有一些規則。
在dockerignore檔案中添加註釋
使用#開頭作為註釋將該行視為註釋。
#This is a comment in the .dockerignore file
忽略Docker上下文或根目錄級別的目錄
dir1 dir2
這將忽略存在於Docker上下文根級別的目錄(dir1和dir2)。請參考上面的目錄結構。
忽略所有檔案和目錄
要排除名稱中包含“file”的所有檔案和目錄。這些檔案將僅來自根目錄。
file*
忽略Docker上下文下一級子目錄
忽略來自根目錄任何子目錄的檔案和目錄。
#using */ to go one level down */file1_dir3.txt
忽略Docker上下文下兩級子目錄
這與上述方法非常相似。
#using */*/ to go two level down */*/file_dir3_1.txt
重新包含檔案
在某些特殊情況下,我們希望包含之前被排除的特定檔案。
*.txt !myfile.txt
假設您正在建立Python容器映象。根目錄包含來自不同貢獻者的所有Python程式,因此您不能刪除它們,並且您必須僅使用該根目錄。您可以執行以下示例以排除所有Python程式,然後重新包含您的Python程式檔案。
步驟1 - 假設根目錄已填充Python檔案
Prg1.py. Prg2.py, Prg3.py …………..and myprogram.py
步驟2 - 建立如下所示的.dockerignore檔案。
#ignore all the python program files *.py #reinclude your python program file !myprogram.py
此過程將更快,無需將不需要的內容COPY/ADD到Docker容器映象中。
實現
在這裡,我們將建立一個完整的環境,該環境將使用Dockerfile、.dockerignore並使用這些檔案構建容器映象。使用的方法部分中提到的相同目錄結構。
. ├── dir1 ├── dir2 ├── dir3 │ ├── dir3_1 │ │ └── file_dir3_1.txt │ ├── dir3_2 │ ├── file1_dir3.txt │ └── file2_dir3.py ├── Dockerfile ├── .dockerignore ├── file1.txt ├── file2.sh ├── file3.py ├── no_ex1 └── No_ex2 5 directories, 10 files
步驟1 - 建立Dockerfile並新增程式碼。
$nano Dockerfile
輸入
#for the base image we are using busybox FROM busybox:latest #set a working directory WORKDIR /my_context_directory #copy everything from the build context to the working directory of the #image COPY . . # list all the copied items RUN ls -l
儲存檔案。
步驟2 - 建立.dockerignore檔案
$nano .dockerignore
輸入
#exclude the file_dir3_1.txt file_dir3_1.txt #exclude file from the root file3.py #exclude a directory one level down */dir3_2
儲存檔案
步驟3 - 構建映象並檢查構建輸出
$docker build –t test_ignore .
輸出
Sending build context to Docker daemon 9.728kB Step 1/4 : FROM busybox:latest ---> 9d5226e6ce3f Step 2/4 : WORKDIR /my_context_directory ---> Running in 49d65f57621d Removing intermediate container 49d65f57621d ---> 386c30f8f511 Step 3/4 : COPY . . ---> 75f65dace2be Step 4/4 : RUN ls -l ---> Running in bd8bbec7d3f9 total 12 -rw-rw-r-- 1 root root 0 Dec 9 03:47 No_ex2 drwxrwxr-x 2 root root 4096 Dec 9 03:47 dir1 drwxrwxr-x 2 root root 4096 Dec 9 03:47 dir2 drwxrwxr-x 3 root root 4096 Dec 9 03:48 dir3 -rw-rw-r-- 1 root root 0 Dec 9 03:46 file1.txt -rw-rw-r-- 1 root root 0 Dec 9 03:46 file2.sh -rw-rw-r-- 1 root root 0 Dec 9 03:47 no_ex1 Removing intermediate container bd8bbec7d3f9 ---> 084bb699260f Successfully built 084bb699260f Successfully tagged test_ignore:latest
輸出顯示,除了dockerignore檔案中的檔案外,所有其他檔案都已複製到容器映象中。
結論
在這篇文章中,我們已經探索了“.dockerignore”檔案的完整360度檢視。清楚地說明了排除、包含以及此檔案的重要性。要掌握該主題,請建立不同的環境並使用上述方法進行實現。