X Tutup
Skip to content

ADD and ENV sub-instruction of Docker ONBUILD instruction not considered while preparing build-context for building image from Dockerfile #219

@hkuyam007

Description

@hkuyam007

Example Dockerfile snippet -

--Set build triggers for adding contents of artifacts, scripts and tests directory to image while building image for component
ONBUILD ADD $SOURCE_ARTIFACT_DIR $ARTIFACT_DIR/
ONBUILD ADD $SOURCE_SCRIPT_DIR $SCRIPT_DIR/
ONBUILD ADD $SOURCE_TEST_DIR $TEST_DIR/

--Set build trigger for copying the articats(jar / war / ear) to tomcat webapps directory from artifact directory
ONBUILD RUN cp $ARTIFACT_DIR/* $TOMCAT_HOME/webapps/


Following code-block in docker-java is not considering the fact that the base image used in the FROM instruction of a Dockerfile may have onbuild instructions like the ones above

Class : Dockerfile$ScannedResult
Default constuctor : Dockerfile$ScannedResult()

Code snippet :
for (DockerfileStatement statement : getStatements()) {
if (statement instanceof DockerfileStatement.Env) {
processEnvStatement((DockerfileStatement.Env) statement);
} else if (statement instanceof DockerfileStatement.Add) {
processAddStatement((DockerfileStatement.Add) statement);
}
}

In the code above only ADD and ENV instructions in the current docker file are being considered.

However the base image used in the Dockerfile may also contain ADD / ENV instructions as part of ONBUILD instructions.

For the time being , I have commented out the code in my local workspace and replaced it with the following code logic -

// adding all files and subfolders in context folder (where Dockerfile is located) to the build context
// this is how docker build works at present
addAllFilesToContext(dockerFile.getParentFile());

// add input folder and all subfolders and files to the file list
private void addAllFilesToContext(File dockerContextFolder) {

    File[] files = dockerContextFolder.listFiles();

    if(files != null && files.length > 0) {

        for(File file : files) {

            filesToAdd.add(file);

            if(file.isDirectory()) {

                addAllFilesToContext(file);

            }

        }

    }

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup