execInContainer method
Runs command inside id and returns its exit code and combined output.
Uses the Docker exec API (POST /containers/{id}/exec + start + inspect).
stdout and stderr are both attached; the combined output is returned as
raw bytes in the second element of the record.
Returns (exitCode, outputBytes).
Implementation
Future<(int, Uint8List)> execInContainer(
String id,
List<String> command,
) async {
final createResp = await _request(
'POST',
'/containers/$id/exec',
body: {
'AttachStdout': true,
'AttachStderr': true,
'Cmd': command,
},
);
_throwIfError(createResp);
final execId =
(createResp.bodyJson as Map<String, dynamic>)['Id'] as String?;
if (execId == null || execId.isEmpty) {
throw StateError(
'Docker API returned an exec create response with no Id field.',
);
}
final startResp = await _request(
'POST',
'/exec/$execId/start',
body: {'Detach': false, 'Tty': false},
);
_throwIfError(startResp);
final inspectResp = await _request('GET', '/exec/$execId/json');
_throwIfError(inspectResp);
final inspectData = inspectResp.bodyJson as Map<String, dynamic>;
final exitCode = inspectData['ExitCode'] as int? ?? 0;
return (exitCode, startResp.body);
}