Ignore Files Across All Subdirectories in .dockerignore

Ignoring files within directories using .dockerignore is a bit different compared to .gitignore.

To ignore a file across all subdirectories, prefix the filename with **. For example, to ignore the file file.txt in all subdirectories, add the following to .dockerignore:


Another example to ignore .DS_Store files on macOS devices:


To ignore a specific file extension across all subdirectories, prefix the file extension with **/*. In this example, all files with the .txt extension will be ignored by Docker:


Also, keep in mind that .dockerignore should be in the root of the context you are passing in to Docker or it won’t be taken into account.