Support ONBUILD instruction in Dockerfiles by orzeh · Pull Request #393 · docker-java/docker-java

@orzeh

I have rewritten the logic that adds files to the build context: now it adds all files in Dockerfile parent directory, except those excluded by .dockerignore.

I have also added some tests that demonstrates the issue, which use ONBUILD in Dockerfile.

I also fixed small bug in .dockerignore test: exclude pattern was wrong according to the Dockerfile doc.

Łukasz Warchał added 2 commits

December 7, 2015 22:21

@orzeh orzeh mentioned this pull request

Dec 7, 2015

@marcuslinke

@orzeh Adding all files to the build context could be problematic if there are huge files. I really don't know how docker CLI handles this, but seems the builder inspects metadata of the base image (http://docs.docker.com/engine/reference/builder/#onbuild):

At the end of the build, a list of all triggers is stored in the image manifest, under the key OnBuild. They can be inspected with the docker inspect command.

Later the image may be used as a base for a new build, using the FROM instruction. As part of processing the FROM instruction, the downstream builder looks for ONBUILD triggers, and executes them in the same order they were registered. If any of the triggers fail, the FROM instruction is aborted which in turn causes the build to fail. If all triggers succeed, the FROM instruction completes and the build continues as usual.

@marcuslinke

@orzeh After thinking about again probably you're right. So uploading all files and ignore only those that matches patterns in .dockerignore. Seems our original implementation that adds only files mentioned in the Dockerfile was some kind of premature optimization then.

@orzeh

@KostyaSha

Wouldn't better place Dockerfile in separate directory?

@orzeh

@KostyaSha

Any :) from my experience i always placing Dockerfile in clean directory to not have accidental uploads to build context.

@KostyaSha

In general PR LGTM 👍 even if i not fully understand ONBUILD feature and all possible cases :)

@orzeh

Ok, now I get it.

I use ONBUILD when I have several Spring Boot microservices and I add them to the image as a exploded JAR to leverage Docker caching (see this blog post). Then I create base image with number of ONBUILD ADD ... instructions and just put FROM my-base-image in microservices Dockerfiles.

marcuslinke

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orzeh Could you explain this change please?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to .dockerignore doc:

the root of the context is considered to be both the working and the root directory

then pattern b excludes all files and directories that are named b in root directory of build context.
However, in this test the file named b is under directory a, so it should not be ignored. After my changes this tests fails.

Pattern */b works as expected (and described in the doc see */temp* example)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, now I got it. Thanks for explanation.

@marcuslinke marcuslinke changed the title Fixes #219 Support ONBUILD instruction in Dockerfiles

Dec 8, 2015

@marcuslinke

@orzeh Thanks for contributing!

@orzeh orzeh mentioned this pull request

May 16, 2016