@Override protected void waitUntilReady() { try { Unreliables.retryUntilTrue((int) startupTimeout.toMillis(), TimeUnit.MILLISECONDS, () -> { CountDownLatch latch = new CountDownLatch(1); FrameConsumerResultCallback callback = new FrameConsumerResultCallback() { @Override public void onNext(Frame frame) { if (frame != null && new String(frame.getPayload()).contains("Connected")) { latch.countDown(); } } }; try ( Closeable __ = dockerClient.logContainerCmd(containerId) .withFollowStream(true) .withSince(0) .withStdErr(true) .exec(callback) ) { return latch.await(1, TimeUnit.SECONDS); } }); } catch (TimeoutException e) { throw new ContainerLaunchException("Timed out waiting for log output", e); } } });
/** * {@inheritDoc} */ public void followOutput(DockerClient dockerClient, String containerId, Consumer<OutputFrame> consumer, OutputFrame.OutputType... types) { final LogContainerCmd cmd = dockerClient.logContainerCmd(containerId) .withFollowStream(true) .withSince(0); final FrameConsumerResultCallback callback = new FrameConsumerResultCallback(); for (OutputFrame.OutputType type : types) { callback.addConsumer(type, consumer); if (type == STDOUT) cmd.withStdOut(true); if (type == STDERR) cmd.withStdErr(true); } cmd.exec(callback); }
resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger())); resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger())); dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback);
@Test public void runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally() { final DockerClientFactory dockFactory = DockerClientFactory.instance(); try { //remove tiny image, so it will be pulled during next command run dockFactory.client() .removeImageCmd(TestcontainersConfiguration.getInstance().getTinyImage()) .withForce(true).exec(); } catch (NotFoundException ignored) { // Do not fail if it's not pulled yet } dockFactory.runInsideDocker( cmd -> cmd.withCmd("sh", "-c", "echo 'SUCCESS'"), (client, id) -> client.logContainerCmd(id) .withStdOut(true) .exec(new LogToStringContainerCallback()) .toString() ); }
.logContainerCmd(container.getId()) .withStdErr(true) .withStdOut(true)
@java.lang.SuppressWarnings("all") public com.github.dockerjava.api.command.LogContainerCmd logContainerCmd(final java.lang.String arg0) { return this.wrappedClient.logContainerCmd(arg0); }
@Override public LogContainerCmd logContainerCmd(String arg0) { return getDelegate().logContainerCmd(arg0); }
public String containerLog(String containerId) { this.readWriteLock.readLock().lock(); try { return dockerClient.logContainerCmd(containerId) .withStdErr(true) .withStdOut(true) .exec(new LogContainerTestCallback()).awaitCompletion().toString(); } catch (InterruptedException e) { log.log(Level.SEVERE, e.getMessage()); return ""; } finally { this.readWriteLock.readLock().unlock(); } }
public String containerLog(String containerId) { this.readWriteLock.readLock().lock(); try { return dockerClient.logContainerCmd(containerId) .withStdErr(true) .withStdOut(true) .exec(new LogContainerTestCallback()).awaitCompletion().toString(); } catch (InterruptedException e) { log.log(Level.SEVERE, e.getMessage()); return ""; } finally { this.readWriteLock.readLock().unlock(); } }
Tail(DockerClient docker, Container container, Logger logger) { Preconditions.checkNotNull(container,"Container must not be null"); this.logger = logger; this.logContainerCmd = docker.logContainerCmd(container.getId()) .withStdErr(true) .withStdOut(true) .withTailAll(); }
@Override public boolean await() { final DockerClient client = dockerClientExecutor.getDockerClient(); final CountDownLatch containerUp = new CountDownLatch(1); try (final LogContainerResultCallback callback = new LogContainerResultCallback(containerUp, this.occurrences)) { client.logContainerCmd(cube.getId()) .withStdErr(true) .withStdOut(true) .withFollowStream(true) .exec(callback); return containerUp.await(this.timeout, TimeUnit.SECONDS); } catch (IOException | InterruptedException e) { logger.log(Level.SEVERE, String.format("Log Await Strategy failed with %s", e.getMessage())); return false; } }
@Override public boolean await() { final DockerClient client = dockerClientExecutor.getDockerClient(); final CountDownLatch containerUp = new CountDownLatch(1); try (final LogContainerResultCallback callback = new LogContainerResultCallback(containerUp, this.occurrences)) { client.logContainerCmd(cube.getId()) .withStdErr(true) .withStdOut(true) .withFollowStream(true) .exec(callback); return containerUp.await(this.timeout, TimeUnit.SECONDS); } catch (IOException | InterruptedException e) { logger.log(Level.SEVERE, String.format("Log Await Strategy failed with %s", e.getMessage())); return false; } }
public String readLogsFully(Integer waitTimeSeconds) throws InterruptedException { final LogContainerCmd logContainerCmd = client.logContainerCmd(containerInfo.getId()) .withStdOut(true) .withFollowStream(true) .withStdErr(true); final StringBuilder stringBuilder = new StringBuilder(); final LogContainerResultCallback resultCallback = new LogContainerResultCallback() { @Override public void onNext(final Frame item) { stringBuilder.append(item.toString() + System.lineSeparator()); } }; logContainerCmd.exec(resultCallback); resultCallback.awaitCompletion(waitTimeSeconds, TimeUnit.SECONDS); return stringBuilder.toString(); }
public void downloadLog(String containerName, Path file) throws IOException { LogContainerRetrieverCallback loggingCallback = new LogContainerRetrieverCallback(file); getClient().logContainerCmd(containerName).withStdErr(true).withStdOut(true) .exec(loggingCallback); try { loggingCallback.awaitCompletion(); } catch (InterruptedException e) { log.warn("Interrupted while downloading logs for container {}", containerName); } }
public void downloadLog(String containerName, Path file) throws IOException { LogContainerRetrieverCallback loggingCallback = new LogContainerRetrieverCallback(file); getClient().logContainerCmd(containerName).withStdErr(true).withStdOut(true) .exec(loggingCallback); try { loggingCallback.awaitCompletion(); } catch (InterruptedException e) { log.warn("Interrupted while downloading logs for container {}", containerName); } }
public static void showLog(DockerClient dockerClient, String containerId, boolean follow, int numberOfLines, LogContainerResultCallback logCallback) { dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).withFollowStream(follow).withTail(numberOfLines).exec(logCallback); }
public void updateStartedForTest() { containersStartedByExtensionPerTest = getContainers().stream() .filter(c -> !containersStartedByExtension.contains(c)) .collect(Collectors.toList()); containersInspectPerTest = containersStartedByExtensionPerTest.stream() .map(c -> dockerClient.inspectContainerCmd(c.getId()).exec()) .collect(Collectors.toList()); logsPerTest = containersStartedByExtensionPerTest.stream() .map(c -> dockerClient.logContainerCmd(c.getId()) .withStdOut(true) .withStdErr(true) .exec(new LogCallback())) .collect(Collectors.toList()); }
@Override public Stream<String> logs(String containerId) { return dockerClient.logContainerCmd(containerId).withFollowStream(true) .withStdOut(true) .withStdErr(true) .exec(new StreamLog()) .stream(); }
public void updateStarted() { containersStartedByExtension = getContainers(); containersInspect = containersStartedByExtension.stream() .map(c -> dockerClient.inspectContainerCmd(c.getId()).exec()) .collect(Collectors.toList()); logs = containersStartedByExtension.stream() .map(c -> dockerClient.logContainerCmd(c.getId()) .withStdOut(true) .withStdErr(true) .exec(new LogCallback())) .collect(Collectors.toList()); }
public static void followOutput(DockerClient dockerClient, String containerId, Consumer<OutputFrame> consumer) { followOutput(dockerClient, containerId, consumer, STDOUT, STDERR); }