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);
}
}
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();
}
}
Aggregations