Skip to content

Build context scan feels slow when directory contains large file tree #1409

Closed

Description

@swiedenfeld

Environment: IntelliJ IDEA 2020.2 EAP Build #IU-202.5103.13 with bundled Docker plugin 202.5103.13. Docker plugin must use docker-java, connection to Docker Desktop for Windows via npipe. Cannot find out the exact version of docker-java that is used.

Given the following minimal project setup:
node_modules with +67k files
Dockerfile with single line FROM hello-world
.dockerignore with single line node_modules

Then, run docker build . in the root directory.

Expected behaviour: The image is built instantly, because build context contains only the Dockerfile (large node_modules directory is ignored), and there are almost no instructions in the Dockerfile itself.

Observed behaviour: For +30 seconds, the screen shows "Building image..." , and after that only one file is added to the build context archive.

I assume, the build traverses all +67k files in the node_modules directory, only to find none of them have to be included. This would be very inefficient. When there are no exceptions, that allow a file below node_modules, it might skip the whole directory.

Update:
Unit test in docker-java-core, testdir has the large folder as child, .dockerignore absent:

@Test
    public void can_parse_dockerfile() throws IOException {
        File dockerFile = new File(".\\testdir\\Dockerfile");
        File baseDirectory = new File(".\\testdir");
        Dockerfile dockerfile = new Dockerfile(dockerFile, baseDirectory);
        Stopwatch stopwatch = Stopwatch.createStarted();
        Dockerfile.ScannedResult parse = dockerfile.parse();
        System.out.println("Parse took " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + "ms");
        System.out.println(parse.filesToAdd.size() + " files added");
    }

Output:

Parse took 28008ms
4 files added

Activity

  1. KostyaSha commented on Jun 7, 2020

    @KostyaSha
    Member

    Try RUN ls if there are files then they were sent to build context into daemon.

    Cannot find out the exact version of docker-java that is used.

    Please fill issue against IDEA first, dockefile parsing was updated few times in different versions.

  2. viatra2 commented on Jun 8, 2020

    @viatra2

    IDEA plugin overrides the com.github.dockerjava.core.dockerfile.Dockerfile in order to provide some feedback in the build log during potentially long archive creation.

    However, the reported issue comes from the recursive call ScannedResult # addFilesInDirectory here.

  3. viatra2 commented on Jun 8, 2020

    @viatra2

    More specifically, this recursion always goes deep into the folder even if the current directory is ignored AND .dockerignore does not have exclusions for any paths below it (AFAIU this exclusion would be something like !directory/evenDeeper/doNotIgnoreMe* entry in the .dockerignore file)

  4. borlander commented on Jun 8, 2020

    @borlander
    Contributor

    AFAIU the Docker CLI code also walks the whole folder structure without further optimizations here

  5. swiedenfeld commented on Jun 8, 2020

    @swiedenfeld
    ContributorAuthor

    @borlander Docker CLI is written in Go, and this is written in Java. I don't think they can be compared. Looking at the code, ScannedResult uses java.io, and it might be a bottleneck traversing the large directory. Maybe java.nio could improve processing?

  6. swiedenfeld commented on Jun 12, 2020

    @swiedenfeld
    ContributorAuthor

    I did a bit of research on this.

    At first glance, the idea of @viatra2 in #1409 (comment) sounds correct and promising.

    Following, I implemented a file walk with java.nio.file.Files.walkFileTree, but it did not traverse the tree considerably faster.

    Then, I did a short circuit that prevents the node_modules to be traversed further if it is ignored. But the exception rules ! rules bit me. There is a lot of combinations of exclude and include lines possible, plus their order in .dockerignore matters. It may thus be inevitable to visit each file and check their effective inclusion/exclusion.

    Nevertheless, one possible improvement I found is #1412.

  7. added a commit that references this issue on Jun 25, 2020
  8. stale commented on Jul 12, 2020

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  9. changed the title [-]Possible performance issue in docker build step[/-] [+]Build context scan feels slow when directory contains large file tree[/+] on Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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