/** * {@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() ); }
public void copyLog(String containerId, boolean follow, boolean stdout, boolean stderr, boolean timestamps, int tail, OutputStream outputStream) throws IOException { this.readWriteLock.readLock().lock(); try { LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId).withStdErr(false).withStdOut(false); logContainerCmd.withFollowStream(follow); logContainerCmd.withStdOut(stdout); logContainerCmd.withStdErr(stderr); logContainerCmd.withTimestamps(timestamps); if (tail < 0) { logContainerCmd.withTailAll(); } else { logContainerCmd.withTail(tail); } OutputStreamLogsResultCallback outputStreamLogsResultCallback = new OutputStreamLogsResultCallback(outputStream); logContainerCmd.exec(outputStreamLogsResultCallback); try { outputStreamLogsResultCallback.awaitCompletion(); } catch (InterruptedException e) { throw new IOException(e); } } finally { this.readWriteLock.readLock().unlock(); } }
.withStdErr(true) .withStdOut(true) .withTimestamps(true) .withTailAll() .withSince(since) .exec(loggingCallback); } catch (com.github.dockerjava.api.exception.ConflictException | com.github.dockerjava.api.exception.NotFoundException e) {
@java.lang.SuppressWarnings("all") public static Optional<String> getDefaultGateway() { java.lang.Object value = DockerClientConfigUtils.defaultGateway.get(); if (value == null) { synchronized (DockerClientConfigUtils.defaultGateway) { value = DockerClientConfigUtils.defaultGateway.get(); if (value == null) { final Optional<String> actualValue = Optional.ofNullable(DockerClientFactory.instance().runInsideDocker(cmd -> cmd.withCmd("sh", "-c", "ip route|awk \'/default/ { print $3 }\'"), (client, id) -> { try { LogToStringContainerCallback loggingCallback = new LogToStringContainerCallback(); client.logContainerCmd(id).withStdOut(true).withFollowStream(true).exec(loggingCallback).awaitStarted(); loggingCallback.awaitCompletion(3, SECONDS); return loggingCallback.toString(); } catch (Exception e) { log.warn("Can\'t parse the default gateway IP", e); return null; } })).map(StringUtils::trimToEmpty).filter(StringUtils::isNotBlank); value = actualValue == null ? DockerClientConfigUtils.defaultGateway : actualValue; DockerClientConfigUtils.defaultGateway.set(value); } } } return (Optional<String>) (value == DockerClientConfigUtils.defaultGateway ? null : value); } }
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(); }
resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger())); resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger())); dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback);
public void start() { logContainerCmd.exec(this); } }
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 void copyLog(String containerId, boolean follow, boolean stdout, boolean stderr, boolean timestamps, int tail, OutputStream outputStream) throws IOException { this.readWriteLock.readLock().lock(); try { LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId).withStdErr(false).withStdOut(false); logContainerCmd.withFollowStream(follow); logContainerCmd.withStdOut(stdout); logContainerCmd.withStdErr(stderr); logContainerCmd.withTimestamps(timestamps); if (tail < 0) { logContainerCmd.withTailAll(); } else { logContainerCmd.withTail(tail); } OutputStreamLogsResultCallback outputStreamLogsResultCallback = new OutputStreamLogsResultCallback(outputStream); logContainerCmd.exec(outputStreamLogsResultCallback); try { outputStreamLogsResultCallback.awaitCompletion(); } catch (InterruptedException e) { throw new IOException(e); } } finally { this.readWriteLock.readLock().unlock(); } }
result = executeLogContainerRequest(client, message).exec(new LogContainerResultCallback() { @Override public void onNext(Frame item) {
@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); } } });
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(); } }
logContainerCmd.withFollowStream(followStream); logContainerCmd.withStdErr(stdErr); logContainerCmd.withStdOut(stdOut); logContainerCmd.withTail(tail); logContainerCmd.withTailAll(); logContainerCmd.withTimestamps(timestamps);
docker .logContainerCmd(container.getId()) .withStdErr(true) .withStdOut(true) .withFollowStream(true) .withTailAll() .exec(loggingCallback) .awaitStarted();
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 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); } }
@Override public Stream<String> logs(String containerId) { return dockerClient.logContainerCmd(containerId).withFollowStream(true) .withStdOut(true) .withStdErr(true) .exec(new StreamLog()) .stream(); }