clue / reactphp-docker

Async, event-driven access to the Docker Engine API, built on top of ReactPHP.
https://clue.engineering/2019/introducing-reactphp-docker
MIT License
107 stars 16 forks source link

How to atomically read a binary file from container? #69

Closed vasily-kartashov closed 3 years ago

vasily-kartashov commented 3 years ago

Given a container ID (let's say "123abd" and file path "/etc/screenshot.jpg"), is there a way to get the content of the file from outside the container?

At the moment it looks like following:

$loop = Factory::create();
$client = new Client($loop, 'http://host.docker.internal:2375');
$payload = null;
$stream = $client->containerArchiveStream($containerId, $filePath);
$stream->on('data', function (string $data) use (&$payload): void {
    $payload = $data;
});
await(buffer($stream), $loop);
return $payload;

What's the proper way to get the complete content of the file before sending it back from a synchronous method?

clue commented 3 years ago

It looks like you're already pretty close. The containerArchiveStream() method returns a stream of the archive, whereas the containerArchive() method returns a promise with the buffered contents of the archive. In your case, you may prefer awaiting the result of this promise instead.

Note that Docker always returns a TAR for the requested file(s). Accessing individual files in the TAR file format string or stream is out of scope for this library. Several libraries are available, one that is known to work is clue/reactphp-tar.

See also https://github.com/clue/reactphp-docker#tar-streaming for more details 👍