The .dockerignore file

If you are familiar with using .gitignore files, then you will already have a basic understanding of the purpose for the .dockerignore file. The .dockerignore file is used to exclude files that you do not want to be included with the build context during a docker image build. Using it helps to prevent sensitive and other unwanted files from being included in the build context, and potentially in the docker image. It is an excellent tool to help keep your Docker images small.

The .dockerignore file needs to be in the root folder of the build context. Like a .gitignore file, it uses a newline-separated list of patterns. Comments in the .dockerignore file are denoted by a # as the first character of a line. You can override a pattern by including an exception line. An exception line is denoted with a ! as the first character of the line. All other lines are considered patterns to use to exclude files and/or folders.

Line order in the .dockerignore file is significant. Matching patterns of lines later in the file will override matching lines earlier in the file. If you add a pattern that matches the .dockerignore file or the Dockerfile file, they will still be sent to the docker daemon with the build context, but they will not be available to any ADD or COPY instructions, and therefore cannot end up in the resulting image. Here is an example:

# Example of a .dockerignore file
# Exclude unwanted files
**/*~
**/*.log
**/.DS_Store
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset