Table of Contents
What is .dockerignore
file?
The .dockerignore
file is a special text file within the Docker context that is used to specify a list of files to be excluded from the Docker context while building the Docker image. Once we execute the docker build
command, the Docker client will package the entire build context as a TAR archive and upload it to the Docker daemon. When we execute the docker build
command, the first line of the output is Sending build context to Docker daemon
, which indicates that the Docker client is uploading the build context to the Docker daemon:
Sending build context to Docker daemon 18.6MB Step 1/5 : FROM ubuntu:focal
Each time we build the Docker image, the build context will be sent to the Docker daemon. As this will take time and bandwidth during the Docker image build process, it is recommended to exclude all the files that are not needed in the final Docker image. The .dockerignore
file can be used to achieve this purpose. In addition to saving time and bandwidth, the .dockerignore
file is used to exclude the confidential files, such as password files and key files from the build context.
How to create a .dockerignore file?
The .dockerignore
file should be created in the root directory of the build context. Before sending the build context to the Docker daemon, the Docker client will look for the this file in the root of the build context.
The docker CLI will always look for this file before sending the context to the docker daemon to build the image. In case this file is present at the root of the directory, the CLI modifies the daemon context to exclude files and directories that match the pattern present in the file.
In this section let us take a look at how the .dockerignore
file is written with an example.
# comment */temp* */*/temp* temp?
Let us understand each line of the .dockerignore
file
- If a line starts with # then it is a comment and will be ignored by the CLI or not executed.
- In this example, we are going to exclude all the files and directories with the name starting with temp in any of the directories and subdirectories in the repository.
You can also make certain exceptions by using !
mark. For example, if you want to exclude all files except for one file, you can do it as shown below.
*.md !README.md
This will exclude all files except the README.md
file from the context.
Example of using .dockerignore file
Few more examples below to implement the .dockerignore
file
# ignore .git and .cache folders .git .cache
Further, you can add files such as .git
, test results, build logs, temporary files, etc to the .dockerignore
file as they are not really required to be built as part of the docker build process.
In case you have a python project, here is what you can do to efficiently build the docker image.
You can exclude cache files such as pycache and files such as pip-log.
__pycache__ *.pyc pip-log.txt .cache *.log .git .mypy_cache .pytest_cache
Here we have a simple directory structure which we use to build our docker image.
$ tree -a -I .git . ├── .dockerignore ├── .gitignore ├── Dockerfile ├── index.js ├── package.json └── supervisord └── conf.d ├── node.conf └── supervisord.conf
The .dockerignore
file allows you to define files and directories that you do not want to upload to the Docker host when you are building the image. In this instance, the .dockerignore
file contains the following lines:
.git
This instructs docker image build
to exclude the .git
directory, which contains the whole source code repository, from the build. The rest of the files reflect the current state of your source code on the checked-out branch. You don’t need the contents of the .git
directory to build the Docker image, and since it can grow quite large over time, you don’t want to waste time copying it every time you do a build.
Conclusion
It is important for the developers to understand the concept of the .dockerignore
file as it helps improve the docker build process, reducing the docker image size and avoiding exposure of secrets or any kind of sensitive information as well.
References
Dockerfile reference
How do you add items to .dockerignore?