question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I get the output?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
LeslieMurphycommented, Mar 10, 2018

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;
	}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to execute a command and get the output ... - Tutorialspoint
You can use the popen and pclose functions to pipe to and from processes. The popen() function opens a process by creating a...
Read more >
How can i get output in my HTML page using Javascript
The id in <input type="text" id="Password" /> has an upper case P, while in javascript you are calling it with a lower case...
Read more >
How to see output of C program in Linux or UNIX - nixCraft
Task: Execute program to see output. Above command will create a file called a.out. To see output of test.c program type: $ ./a.out ......
Read more >
Basic Input, Output, and String Formatting in Python
In this tutorial, you'll learn about Python input and output. ... Output can be displayed directly to the console or IDE, to the...
Read more >
How can I get the output and test the dig command?
The following command will capture the output of the IP address and put it into a shell variable. $ IP=$(dig +short www.google.com |...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found