com.github.dockerjava.core.command.LogContainerResultCallback Java Examples

The following examples show how to use com.github.dockerjava.core.command.LogContainerResultCallback. 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: ContainerLogs.java    From OpenLabeler with Apache License 2.0 7 votes vote down vote up
public List<String> getDockerLogs() {

        final List<String> logs = new ArrayList<>();

        LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId);
        logContainerCmd.withStdOut(true).withStdErr(true);
        logContainerCmd.withSince( lastLogTime );  // UNIX timestamp (integer) to filter logs. Specifying a timestamp will only output log-entries since that timestamp.
        // logContainerCmd.withTail(4);  // get only the last 4 log entries

        logContainerCmd.withTimestamps(true);

        try {
            logContainerCmd.exec(new LogContainerResultCallback() {
                @Override
                public void onNext(Frame item) {
                    logs.add(item.toString());
                }
            }).awaitCompletion();
        } catch (InterruptedException e) {
            LOG.severe("Interrupted Exception!" + e.getMessage());
        }

        lastLogTime = (int) (System.currentTimeMillis() / 1000) + 5;  // assumes at least a 5 second wait between calls to getDockerLogs

        return logs;
    }
 
Example #2
Source File: DockerHelper.java    From dynamo-cassandra-proxy with Apache License 2.0 6 votes vote down vote up
public void startDSE() {

        String img = "datastax/ddac";
        String tag = "latest";
        String name = "ddac";
        List<Integer> ports = Arrays.asList(9042);
        List<String> volumeDescList = Arrays.asList();;
        List<String> envList = Arrays.asList("DS_LICENSE=accept");
        List<String> cmdList = Arrays.asList();

        String containerId = startDocker(img,tag,name, ports,volumeDescList, envList, cmdList);

        LogContainerResultCallback loggingCallback = new
                LogContainerResultCallback();

        waitForPort("localhost",9042, Duration.ofMillis(50000), logger, true);

    }
 
Example #3
Source File: ChaosContainer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public void tailContainerLog() {
    CompletableFuture.runAsync(() -> {
        while (null == containerId) {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                return;
            }
        }

        LogContainerCmd logContainerCmd = this.dockerClient.logContainerCmd(containerId);
        logContainerCmd.withStdOut(true).withStdErr(true).withFollowStream(true);
        logContainerCmd.exec(new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                log.info(new String(item.getPayload(), UTF_8));
            }
        });
    });
}
 
Example #4
Source File: ChaosContainer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public String getContainerLog() {
    StringBuilder sb = new StringBuilder();

    LogContainerCmd logContainerCmd = this.dockerClient.logContainerCmd(containerId);
    logContainerCmd.withStdOut(true).withStdErr(true);
    try {
        logContainerCmd.exec(new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                sb.append(new String(item.getPayload(), UTF_8));
            }
        }).awaitCompletion();
    } catch (InterruptedException e) {

    }
    return sb.toString();
}
 
Example #5
Source File: DockerContainersUtil.java    From minimesos with Apache License 2.0 6 votes vote down vote up
/**
 * Synchronized method for returning logs of docker container
 *
 * @param containerId - ID of the container ot lookup logs
 * @return list of strings, where every string is log line
 */
public static List<String> getDockerLogs(String containerId) {

    final List<String> logs = new ArrayList<>();

    LogContainerCmd logContainerCmd = DockerClientFactory.build().logContainerCmd(containerId);
    logContainerCmd.withStdOut(true).withStdErr(true);
    try {
        logContainerCmd.exec(new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
                logs.add(item.toString());
            }
        }).awaitCompletion();
    } catch (InterruptedException e) {
        throw new MinimesosException("Failed to retrieve logs of container " + containerId, e);
    }

    return logs;
}
 
Example #6
Source File: BesuNode.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
private void showLogFromBesuContainer() {
  docker
      .logContainerCmd(besuContainerId)
      .withStdOut(true)
      .withStdErr(true)
      .withFollowStream(true)
      .withTail(500)
      .exec(
          new LogContainerResultCallback() {
            @Override
            public void onNext(Frame item) {
              LOG.info(item.toString());
            }
          });
}
 
Example #7
Source File: DockerTestUtils.java    From module-ballerina-docker with Apache License 2.0 4 votes vote down vote up
/**
 * Start a docker container and wait until a ballerina service starts.
 *
 * @param containerID ID of the container.
 * @param logToWait   Log message to confirm waiting
 * @return true if service started, else false.
 */
public static boolean startContainer(String containerID, String logToWait) throws DockerTestException {
    try {
        DockerClient dockerClient = getDockerClient();
        log.info("Starting container: " + containerID);

        dockerClient.startContainerCmd(containerID).exec();

        int logWaitCount = 0;
        boolean containerStarted = false;
        StringBuilder containerLogs = new StringBuilder();

        while (logWaitCount < LOG_WAIT_COUNT) {
            log.info("Waiting for container startup " + (logWaitCount + 1) + "/" + LOG_WAIT_COUNT);
            dockerClient.logContainerCmd(containerID)
                    .withStdErr(true)
                    .withStdOut(true)
                    .withFollowStream(true)
                    .withTailAll()
                    .exec(new LogContainerResultCallback() {
                        @Override
                        public void onNext(Frame item) {
                            containerLogs.append((new String(item.getPayload())).trim()).append("\n");
                            super.onNext(item);
                        }
                    }).awaitCompletion(3, TimeUnit.SECONDS);

            if (containerLogs.toString().trim().contains(logToWait)) {
                containerStarted = true;
                break;
            }
            logWaitCount++;
        }

        if (containerStarted) {
            log.info("Container started: " + containerID);

            // Find docker container IP address if such exists
            InspectContainerResponse containerInfo = getDockerClient().inspectContainerCmd(containerID).exec();

            String os = System.getProperty("os.name").toLowerCase(Locale.getDefault());
            // If OS is linux
            if ((os.contains("nix") || os.contains("nux") || os.contains("aix")) &&
                    !"".equals(containerInfo.getNetworkSettings().getIpAddress())) {
                serviceIP = containerInfo.getNetworkSettings().getIpAddress();
            }

            log.info("Container IP address found as: " + serviceIP);

            return true;
        } else {
            log.error("Container did not start: " + containerLogs);
            return false;
        }
    } catch (InterruptedException ex) {
        throw new DockerTestException(ex);
    }
}