pullImage method
Downloads image from its registry, waiting for the end of the pull;
its progress is ignored.
Throws a DockerException saying which image could not be pulled and
why (a name or tag that does not exist, a registry that asks for a
login). Registry credentials are not sent: private images must be
pulled beforehand (docker login, docker pull).
Implementation
Future<void> pullImage(String image) async {
final (name, tag) = parseImage(image);
final HttpClientResponse response;
try {
response = await _send(
'POST',
'/images/create',
query: {'fromImage': name, 'tag': ?tag},
);
} on DockerException catch (e) {
throw DockerException(
"Cannot pull the image '$image': ${e.message}",
statusCode: e.statusCode,
);
}
// The progress is a stream of JSON lines; a failure is one of them.
await for (final line
in response.transform(utf8.decoder).transform(const LineSplitter())) {
if (line.trim().isEmpty) {
continue;
}
final Object? event;
try {
event = jsonDecode(line);
} on FormatException {
continue;
}
if (event is Map<String, Object?> && event['error'] != null) {
throw DockerException(
"Cannot pull the image '$image': ${event['error']}",
);
}
}
}