Dockerignore: Ignore everything except a file and the Dockerfile

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Dockerignore: Ignore everything except a file and the Dockerfile



So the main intention was to dockerize a fat jar application and put it into Elasticbeanstalk. The problem is with the context. It's a little bit stupid to add so much context into docker if all I need is actually a single jar file.



I've been playing around with the .dockerignore file, but I am lost. I tried to use the gitignore negation, but it doesn't work.


*
!Dockerfile
*/
!target/
target/*
!target/*.jar



There's also that thing with regex, but it seems like complicated regex is not supported.


^((?!Dockerfile).)*$



I have also tried searching in stackoverflow, and these two are all I found:



Exceptions in .dockerignore



is there a way to negate a pattern in .dockerignore?



This question might be similiar to the second one, but I think it's slightly difference since in here, I just want to include a single file into the context.



Any help will be appreciated.





Exclamation mark for folders works for me!
– Ivan Aracki
Jun 13 at 11:48






3 Answers
3



From the dockerfile reference:



Beyond Go’s filepath.Match rules, Docker also supports a special wildcard string ** that matches any number of directories (including zero). For example, **/*.go will exclude all files that end with .go that are found in all directories, including the root of the build context.



So a line containing simply ** will ignore everything in the same directory as the Dockerfile.



As expected the exclamation can then be used to reference any files you do wish to send to the docker daemon.





It's extremely important that the ** goes to the top of the file, otherwise the exclusions will be ignored. Docker uses the last rule that matches as the "final" rule, so if ** is last it'll match everything.
– tedivm
May 3 at 4:36


**


**





this doesn't work for files in subdirectories
– Pascalius
Jul 3 at 8:44



This may sound strange, but if all you need is a single jar file, you could create a "docker" folder in your build system that contains your Dockerfile. When you run your builds, have the build scripts copy the single jar file into "docker" then execute the docker image build (from inside the "docker" folder) and push to your docker registry when done.





Hi, it doesn't sound strange at all. I thought it was a general practice, but I was looking for a way to do it with the .dockerignore if possible. Besides, I don't really like mvn xml :)
– Rowanto
Jan 26 '15 at 12:09



If you need to ignore everything except some directories or files and also ignore some unnecessary files inside those allowed directories you can use the following .dockerignore file:


.dockerignore


# Ignore everything
**

# Allow files and directories
!/file.txt
!/src/**

# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

415 Unsupported Media Type while sending json file over REST Template

PHP parse/syntax errors; and how to solve them?