- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to ignore files in Docker?
Introduction
Here in this article, we will learn about the ignore file in docker. How to create a ignore file and how to use it with various ways to add files and folders to the ignore file. All the pre-requisite knowledge is mentioned in a stepwise manner. Do not directly jump to the implementation without knowing the requirement of the ignore files in the code industry.
What are ignored files?
Ignore files created in different systems like in the Git repository and the Dockerifle. These files help to exclude some files and directories from being used or copied in the output. The output for the dockerfile is the image and for the git, it could be the commit. Examples of some most commonly used ignore files in DevOps are “gitignore” and “dockerignore”.
Types of files to be ignored
The main task is to ignore things. These things can be of different types. Like it could be a program file in python or a ReactJS app directory with sub-directories. All these files are mentioned in the below list.
Directory
File − text or program files
Sub−directory
.dockerignore itself
The need of ignoring these files is because we no longer need these files in our final product. These files only need on the Host.
Method
In this section, we will learn about excluding different types of files by adding them to the dockerignore file.
All the files that are going to be in the dockerignore should be mentioned with respect to the docker context directory. Docker context is that directory path where all the useful files that need to be added inside the image are present. Mostly we use . (dot) as the docker context while building the image.
Create a “.dockerignore” file
The “.dockerignore” should be created where your Dockerfile is present otherwise it won’t work. Use the below commands to create and exclude various types of files.
$touch .dockerignore
We have created a directory structure to use in explaining all these methods.
$tree -la
Output
. ├── 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
Open the ignore file to add various files from the directory structure above.
$nano .dockerignore
There are some rules to add different kinds of files, directories, and comments in the docker ignore file.
Add a comment in the dockerignore file
Using a # while starting a comment will consider that line as a comment.
#This is a comment in the .dockerignore file
Ignore a directory at the level of the docker context or root directory
dir1 dir2
This will ignore the directories (dir1 & dir2) that are present at the root level of the docker context. For reference see the above directory structure.
Ignore all the files and directory
To exclude all the files and directories that have “file” in their name. These files will be from the root directory only.
file*
Ignore sub-directory up to one level down of the docker context
Ignore the files as well as the directories from any sub-directories of the root directory.
#using */ to go one level down */file1_dir3.txt
Ignore a sub-directory up to the two-level down of the docker context
This is very similar to the above method.
#using */*/ to go two level down */*/file_dir3_1.txt
To reinclude a file
There are some special cases when we want to include a particular file that was excluded earlier.
*.txt !myfile.txt
Suppose you are creating a python container image. The root directory containers all the python programs from different contributors so you can’t delete them and you have to use that root directory only. You could perform the below example to exclude all the python programs and then reinclude your python program file.
Step 1 − suppose the root directory is filled with pythons files
Prg1.py. Prg2.py, Prg3.py …………..and myprogram.py
Step 2 − Create a .dockerignore file as below.
#ignore all the python program files *.py #reinclude your python program file !myprogram.py
The process will be faster and no need to COPY/ADD unwanted things to the docker container image.
Implementation
Here we are going to create a complete environment that will use the Dockerfile, .dockerignore, and build a container image using these files. The same directory structure is used as mentioned in the method section.
. ├── 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
Step 1 − Create a Dockerfile and add the code.
$nano Dockerfile
Input
#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
Save the file.
Step 2 − Create a .dockerignore file
$nano .dockerignore
Input
#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
Save the file
Step 3 − Build the image and check the build output
$docker build –t test_ignore .
Output
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
The output shows that all the files other than the files in the dockerignore files have been copied to the container image.
Conclusions
Here in this article, we have explored a complete 360-degree view of the “.dockerignore” file. Excluding, including, and the importance of this file is clearly stated. To get hands-on with the topic create different environments and implement with above-mentioned methods.
- Related Articles
- How to ignore hidden files using os.listdir() in Python?
- How to copy files from host to Docker container?
- Copying files from Docker container to Host
- How to reference embedded Docker resource files using file path URL?
- Copy Files from Docker Container to Local Machine
- How to list containers in Docker?
- How to list images in Docker?
- How to ignore an exception and proceed in Python?
- How to Hot-Reload in ReactJS Docker?
- How to ignore a previously committed file in Git repository?
- How to Convert Multiple XLS Files to XLSX Files in Excel?
- How to create Java Priority Queue to ignore duplicates?
- How to make an application ignore screen orientation change in Android?
- How to skip or ignore the execution of tests in TestNG?
- How to Use Volume Sharing in Docker Swarm?
