public void buildToDaemon()

in plugins/docker/component-image/src/main/java/co/elastic/gradle/dockercomponent/JibActions.java [118:184]


    public void buildToDaemon(
            String localDockerDaemonTag,
            RegularFile imageId,
            List<ContainerImageBuildInstruction> instructions,
            Path contextRoot
    ) {
        final Optional<FromLocalArchive> fromLocalImageBuild = instructions.stream()
                .filter(instruction -> instruction instanceof FromLocalArchive)
                .map(it -> (FromLocalArchive) it)
                .findFirst();
        final JibContainerBuilder jibBuilder;
        if (fromLocalImageBuild.isPresent()) {
            jibBuilder = Jib.from(
                    TarImage.at(fromLocalImageBuild.get().archive().get().toPath())
            );
        } else {
            final Optional<From> from = instructions.stream()
                    .filter(instruction -> instruction instanceof From)
                    .map(it -> (From) it)
                    .findFirst();
            if (from.isPresent()) {
                final String baseImageReference = from.get().getReference().get();
                try {
                    jibBuilder = Jib.from(baseImageReference);
                } catch (InvalidImageReferenceException e) {
                    throw new GradleException("Invalid image reference: " + baseImageReference, e);
                }
            } else {
                jibBuilder = Jib.fromScratch();
            }
        }

        jibBuilder.setPlatforms(Set.of(new Platform(Architecture.current().dockerName(), "linux")));

        processInstructions(
                jibBuilder,
                contextRoot,
                instructions
        );

        // We need the image ID to stay constant when the inputs don't change so that we can use it for build avoidance,
        // but the creation time poses some challenges in this regard since it causes it to be always different.
        // Jib builds directly to the local daemon, so we don't really have anything to cache here. While we could
        // build a tar that can be cached and then import it using the cli that will have a very high overhead, require
        // more storage in the cache node and will be slower to run. The process of building the image is deterministic
        // the timestamp is the only reason it changes. The simplest solution is to set the creation time to a fixed
        // value. It can be any value as long as it's fixed. Setting it to the unix epoch is the most likely to signal
        // that this is not set. Note that images pushed to the registry don't take this code path and will have a correct
        // creation time.
        jibBuilder.setCreationTime(Instant.ofEpochMilli(0));
        try {
            final JibContainer container;

            container = jibBuilder.containerize(
                    Containerizer.to(DockerDaemonImage.named(localDockerDaemonTag))
                            .setApplicationLayersCache(getJibApplicationCacheDir())
                            .setBaseImageLayersCache(getJibBaseLayersCacheDir())
            );

            Files.writeString(
                    imageId.getAsFile().toPath(),
                    container.getImageId().getHash()
            );
        } catch (IOException | InvalidImageReferenceException | InterruptedException | RegistryException | CacheDirectoryCreationException | ExecutionException e) {
            throw new GradleException("Failed to import component image", e);
        }
    }