Search in sources :

Example 1 with BuildImageResultCallback

use of com.github.dockerjava.api.command.BuildImageResultCallback in project testcontainers-java by testcontainers.

the class ImageFromDockerfile method resolve.

@Override
protected final String resolve() {
    Logger logger = DockerLoggerFactory.getLogger(dockerImageName);
    DockerClient dockerClient = DockerClientFactory.instance().client();
    try {
        if (deleteOnExit) {
            ResourceReaper.instance().registerImageForCleanup(dockerImageName);
        }
        BuildImageResultCallback resultCallback = new BuildImageResultCallback() {

            @Override
            public void onNext(BuildResponseItem item) {
                super.onNext(item);
                if (item.isErrorIndicated()) {
                    logger.error(item.getErrorDetail().getMessage());
                } else {
                    logger.debug(StringUtils.chomp(item.getStream(), "\n"));
                }
            }
        };
        // We have to use pipes to avoid high memory consumption since users might want to build really big images
        @Cleanup PipedInputStream in = new PipedInputStream();
        @Cleanup PipedOutputStream out = new PipedOutputStream(in);
        BuildImageCmd buildImageCmd = dockerClient.buildImageCmd(in);
        configure(buildImageCmd);
        Map<String, String> labels = new HashMap<>();
        if (buildImageCmd.getLabels() != null) {
            labels.putAll(buildImageCmd.getLabels());
        }
        labels.putAll(DockerClientFactory.DEFAULT_LABELS);
        // noinspection deprecation
        labels.putAll(ResourceReaper.instance().getLabels());
        buildImageCmd.withLabels(labels);
        prePullDependencyImages(dependencyImageNames);
        BuildImageResultCallback exec = buildImageCmd.exec(resultCallback);
        long bytesToDockerDaemon = 0;
        // To build an image, we have to send the context to Docker in TAR archive format
        try (TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(new GZIPOutputStream(out))) {
            tarArchive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
            tarArchive.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
            for (Map.Entry<String, Transferable> entry : transferables.entrySet()) {
                Transferable transferable = entry.getValue();
                final String destination = entry.getKey();
                transferable.transferTo(tarArchive, destination);
                bytesToDockerDaemon += transferable.getSize();
            }
            tarArchive.finish();
        }
        log.info("Transferred {} to Docker daemon", FileUtils.byteCountToDisplaySize(bytesToDockerDaemon));
        if (// warn if >50MB sent to docker daemon
        bytesToDockerDaemon > FileUtils.ONE_MB * 50)
            log.warn("A large amount of data was sent to the Docker daemon ({}). Consider using a .dockerignore file for better performance.", FileUtils.byteCountToDisplaySize(bytesToDockerDaemon));
        exec.awaitImageId();
        return dockerImageName;
    } catch (IOException e) {
        throw new RuntimeException("Can't close DockerClient", e);
    }
}
Also used : BuildResponseItem(com.github.dockerjava.api.model.BuildResponseItem) DockerClient(com.github.dockerjava.api.DockerClient) HashMap(java.util.HashMap) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) Logger(org.slf4j.Logger) Cleanup(lombok.Cleanup) BuildImageResultCallback(com.github.dockerjava.api.command.BuildImageResultCallback) GZIPOutputStream(java.util.zip.GZIPOutputStream) BuildImageCmd(com.github.dockerjava.api.command.BuildImageCmd) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BuildImageResultCallback

use of com.github.dockerjava.api.command.BuildImageResultCallback in project synopsys-detect by blackducksoftware.

the class BuildDockerImageProvider method installImage.

@Override
public void installImage(String imageName, DockerClient dockerClient) {
    File imageDockerFile = FileUtil.asFile(DetectorBatteryTestRunner.class, dockerfileResourceName, "/docker/");
    Assertions.assertNotNull(imageDockerFile, "Could not find the dockerfile in the resources, ensure the dockerfile exists as named. It is needed to build the image if the image is not present.");
    try (BuildImageCmd buildImageCmd = dockerClient.buildImageCmd(imageDockerFile)) {
        buildImageCmd.withTags(Bds.of(imageName).toSet()).exec(new BuildImageResultCallback()).awaitImageId();
    }
}
Also used : BuildImageResultCallback(com.github.dockerjava.api.command.BuildImageResultCallback) BuildImageCmd(com.github.dockerjava.api.command.BuildImageCmd) File(java.io.File)

Aggregations

BuildImageCmd (com.github.dockerjava.api.command.BuildImageCmd)2 BuildImageResultCallback (com.github.dockerjava.api.command.BuildImageResultCallback)2 DockerClient (com.github.dockerjava.api.DockerClient)1 BuildResponseItem (com.github.dockerjava.api.model.BuildResponseItem)1 File (java.io.File)1 IOException (java.io.IOException)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Cleanup (lombok.Cleanup)1 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)1 Logger (org.slf4j.Logger)1