Java Code Examples for com.github.dockerjava.core.command.BuildImageResultCallback#awaitImageId()
The following examples show how to use
com.github.dockerjava.core.command.BuildImageResultCallback#awaitImageId() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CreateImageCommand.java From cubeai with Apache License 2.0 | 4 votes |
@Override public void execute() throws DockerException { if (dockerFolder == null) { logger.error( "dockerFolder is not configured"); throw new IllegalArgumentException("dockerFolder is not configured"); } if (imageName == null) { logger.error( "imageName is not configured"); throw new IllegalArgumentException("imageName is not configured"); } if (imageTag == null) { logger.error( "imageName is not configured"); throw new IllegalArgumentException("imageTag is not configured"); } if (!dockerFolder.exists()) { logger.error( "configured dockerFolder '" + dockerFolder + "' does not exist."); throw new IllegalArgumentException("configured dockerFolder '" + dockerFolder + "' does not exist."); } final Map<String, String> buildArgsMap = new HashMap<String, String>(); if ((buildArgs != null) && (!buildArgs.trim().isEmpty())) { logger.debug("Parsing buildArgs: " + buildArgs); String[] split = buildArgs.split(",|;"); for (String arg : split) { String[] pair = arg.split("="); if (pair.length == 2) { buildArgsMap.put(pair[0].trim(), pair[1].trim()); } else { logger.error("Invalid format for " + arg + ". Buildargs should be formatted as key=value"); } } } String dockerFile = this.dockerFile == null ? "Dockerfile" : this.dockerFile; File docker = new File(dockerFolder, dockerFile); if (!docker.exists()) { logger.error( "Configured Docker file '%s' does not exist. {}", dockerFile); throw new IllegalArgumentException(String.format("Configured Docker file '%s' does not exist.", dockerFile)); } DockerClient client = getClient(); try { BuildImageResultCallback callback = new BuildImageResultCallback() { @Override public void onNext(BuildResponseItem item) { super.onNext(item); } }; BuildImageCmd buildImageCmd = client.buildImageCmd(docker) .withTags(new HashSet<>(Arrays.asList(imageName + ":" + imageTag))).withNoCache(noCache) .withRemove(rm); // .withTag(imageName + ":" + imageTag) if (!buildArgsMap.isEmpty()) { for (final Map.Entry<String, String> entry : buildArgsMap.entrySet()) { buildImageCmd = buildImageCmd.withBuildArg(entry.getKey(), entry.getValue()); } } BuildImageResultCallback result = buildImageCmd.exec(callback); this.imageId = result.awaitImageId(); } catch (Exception e) { logger.error( "Error {}", e.getMessage()); throw new RuntimeException(e); } }
Example 2
Source File: ImageFromDockerfile.java From testcontainers-java with MIT License | 4 votes |
@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); 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); 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 (bytesToDockerDaemon > FileUtils.ONE_MB * 50) // warn if >50MB sent to docker daemon 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); } }