execCreate($container, $cmd)->then(function ($info) use ($client, $out, $stderr, &$exit) { $stream = $client->execStartStream($info['Id'], false, 'stderr'); $stream->pipe($out); // forward custom stderr event to STDERR stream $stream->on('stderr', function ($data) use ($stderr, $stream) { if ($stderr->write($data) === false) { $stream->pause(); $stderr->once('drain', function () use ($stream) { $stream->resume(); }); } }); $stream->on('error', 'printf'); // remember exit code of executed command once it closes $stream->on('close', function () use ($client, $info, &$exit) { $client->execInspect($info['Id'])->then(function ($info) use (&$exit) { $exit = $info['ExitCode']; }, 'printf'); }); }, 'printf'); $loop->run(); exit($exit);