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.

container.logs().fetch() returning non-ASCII value

See original GitHub issue

Hi Everyone,

I am running ubuntu image with command as date.

docker logs is showing correct output but my program is not able to print anything.

Program:

public class App {
	public static void main(String[] args) throws Exception {
		final Container container = new LocalDocker(new File("/var/run/docker.sock")).containers().create("ubuntutest",
				containerJsonObject());
		container.start();

		while (runningState(container)) {

		}
		final String logs = container.logs().fetch();
		logs.trim();
		System.out.println(logs);
		System.out.println(logs.length());
	}

	private static JsonObject containerJsonObject() {
		JsonObjectBuilder json = Json.createObjectBuilder();
		json.add("AttachStdout", true);
		json.add("AttachStderr", true);
		json.add("Image", "ubuntu");
		json.add("Tty", false);
		json.add("Cmd", "date");
		JsonArrayBuilder builder = Json.createArrayBuilder();
		builder.add("OUT=/scans/firstimg.jpg");
		json.add("Env", builder);
		JsonObjectBuilder hostConfig = Json.createObjectBuilder();
		JsonArrayBuilder binds = Json.createArrayBuilder();
		binds.add("/home/shubham/scans:/scans/");
		hostConfig.add("Binds", binds);
		hostConfig.add("Privileged", true);
		json.add("HostConfig", hostConfig);
		return json.build();
	}

	private static boolean runningState(final Container container) throws IOException {
		return container.inspect().getJsonObject("State").getBoolean("Running");
	}

}

Output of above is:



37

Thanks for any help!!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tuxjicommented, Apr 17, 2020

Yes, the new 0.0.12 version fixes the problem really well and the new Container.wait() method is wonderful too! Thank you!

1reaction
tuxjicommented, Mar 17, 2020

I just found the same problem, and I know what the problem is. When you call GET /containers/{id}/logs to get stdout and stderr logs from a container, the logs are returned using a binary stream format in the response body. The stream format is described in the documentation for the attach endpoint. Briefly summarizing, eight binary bytes are prepended to each line of logging output to tell you 1) whether the next line came from stdout or stderr, and 2) how many bytes long the next line is.

Here are two example lines taken from my Emacs buffer visiting a file saved by wget ‘http://localhost:2375/v1.35/containers/{id}/logs?stdout=true&stderr=true’:

^A^@^@^@^@^@^@,Info: STEM is running. Please be patient...
^B^@^@^@^@^@^@c[main] INFO kbaseRoot set to /app/STEM

The first byte is a ^A or ^B (1 or 2) followed by six zero (null) bytes, then followed by a byte which looks like an ordinary character but really is meant to be the size of the next line, and then the line itself is appended. I think it would be easier for callers if fetch() trimmed these eight bytes from the beginning of each line in the string output it reads from the logs API endpoint, but one could disagree, simply document the stream format in the javadoc, and make callers decode the stream format themselves.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fetch a non-ascii url with urlopen?
To convert an IRI to a plain ASCII URI: non-ASCII characters in the hostname part of the address have to be encoded using...
Read more >
fetch fails on ByteString header with non-ASCII chars #1317
toString("utf8") , but it may return a String with non Latin 1 characters — with 256 char codes and more, so normalizeAndValidateHeaderValue ...
Read more >
Manage Unicode Characters in Data Using T-SQL
Collations control only attributes such as comparison rules and case sensitivity. This T-SQL statement prints the ASCII values and characters ...
Read more >
Get Blob (REST API) - Azure Storage
If you call Get Blob on a page blob with no range specified, the service returns the range of pages up to the...
Read more >
Retrieve selected fields from a search | Elasticsearch ...
The fields parameter also does not guarantee that array values are returned in a specific order. See the mapping documentation on arrays for...
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