Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerignore: Ignore everything except a file and the Dockerfile

Tags:

docker

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.

like image 645
Rowanto Avatar asked Jan 22 '15 19:01

Rowanto


People also ask

Should Dockerignore ignore Dockerfile?

We suggest you include the Dockerfile in the Docker image (i.e. not mention it in . dockerignore) as it can help the consumers of the image to understand how it was build. Before you do that, make sure that you Dockerfile does not contain any sensitive information.

What should I ignore in Dockerignore?

You might not want to expose such important files into the final docker image. For example, exposing your . git folder inside your docker image. Thus, it's always recommended to ignore such files and folders by mentioning them into .

Should Dockerignore be same as Gitignore?

dockerignore must be a superset of . gitignore . Docker ignore contains files which you want Docker build to ignore and in some cases it could be your source code as well.


2 Answers

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:

# 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 
like image 51
Mauricio Sánchez Avatar answered Sep 20 '22 01:09

Mauricio Sánchez


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.

like image 35
abraund Avatar answered Sep 20 '22 01:09

abraund