Docker-java: How can I get the output?

Created on 13 Nov 2016  路  7Comments  路  Source: docker-java/docker-java

I can't find the way to get the output from the container

public static void main(String[] args) throws InterruptedException {
        DockerClient docker = DockerClientBuilder
            .getInstance("tcp://127.0.0.1:2376")
            .build();

        // Run
        CreateContainerResponse container = docker
            .createContainerCmd("ubuntu")
            .withCmd("/bin/sh", "-c", "echo Hello world!")
            .withTty(true)
            .exec();

        // Log
        LogContainerResultCallback loggingCallback = new
            LogContainerResultCallback();

        docker
            .logContainerCmd(container.getId())
            .withStdErr(true)
            .withStdOut(true)
            .withFollowStream(true)
            .withTailAll()
            .exec(loggingCallback)
            .awaitStarted();

        loggingCallback.awaitCompletion(3, TimeUnit.SECONDS);

        String output = loggingCallback.toString();
        System.out.println(output);
    }
typquestion

Most helpful comment

This works for me:

public class GetContainerLog {
    private DockerClient dockerClient;
    private String containerId;
    private int lastLogTime;

    private static String nameOfLogger = "dockertest.PrintContainerLog";
    private static Logger myLogger = Logger.getLogger(nameOfLogger);

    public GetContainerLog(DockerClient dockerClient, String containerId) {
        this.dockerClient = dockerClient;
        this.containerId = containerId;
        this.lastLogTime = (int) (System.currentTimeMillis() / 1000);
    }

    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) {
            myLogger.severe("Interrupted Exception!" + e.getMessage());
        }

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

        return logs;
    }

All 7 comments

After creating a container you have to start it with docker.startContainerCmd(...).

@marcuslinke Thanks, but I still don't know how to get the output from the LogContainerResultCallback

Take a look at the appropriate test cases for LogContainerCmd.

This works for me:

public class GetContainerLog {
    private DockerClient dockerClient;
    private String containerId;
    private int lastLogTime;

    private static String nameOfLogger = "dockertest.PrintContainerLog";
    private static Logger myLogger = Logger.getLogger(nameOfLogger);

    public GetContainerLog(DockerClient dockerClient, String containerId) {
        this.dockerClient = dockerClient;
        this.containerId = containerId;
        this.lastLogTime = (int) (System.currentTimeMillis() / 1000);
    }

    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) {
            myLogger.severe("Interrupted Exception!" + e.getMessage());
        }

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

        return logs;
    }
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);
}

This will also work.

This works for me:

public class GetContainerLog {
  private DockerClient dockerClient;
  private String containerId;
  private int lastLogTime;

  private static String nameOfLogger = "dockertest.PrintContainerLog";
  private static Logger myLogger = Logger.getLogger(nameOfLogger);

  public GetContainerLog(DockerClient dockerClient, String containerId) {
      this.dockerClient = dockerClient;
      this.containerId = containerId;
      this.lastLogTime = (int) (System.currentTimeMillis() / 1000);
  }

  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) {
          myLogger.severe("Interrupted Exception!" + e.getMessage());
      }

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

      return logs;
  }

LogContainerResultCallback is deprecated. do we have a similar solution for this?

Was this page helpful?
0 / 5 - 0 ratings