There is no built-in way to do this. However, with a little hacking you can create an "inverse dockerignore file" like this:
include.sh
#!/usr/bin/env sh
FILE=.dockerignore
# add git indexed files
git ls-files > $FILE
# add NOT symbol in front of each line to keep
sed -i 's/^/!/' $FILE
# add * as the first line in the file
sed -i '1s/^/*\n&/' $FILE
.dockerignore
The result is something like this:
*
!.dockerignore
!.gitignore
!Dockerfile
!LICENSE
!README.md
!bin/action.sh
!bin/commitlint
!commitlint-plugin-tense/blacklist.txt
!commitlint-plugin-tense/index.js
!commitlint-plugin-tense/index.test.js
!commitlint-plugin-tense/package-lock.json
!commitlint-plugin-tense/package.json
!commitlint.config.js
!package.json
!yarn.lock
Conclusion
The first line *
ignores all files. Each subsequent line in the prefixed with !
tells Docker to NOT ignore the given file. The result is essentially a whitelist of files to add using COPY
directive in a Dockerfile
. The main thing to watch out for is that this file can quickly become obsolete when adding or removing files and would have to be constantly maintained.