From ce95d1b8baeaa954ce328259571c5563a1b9c935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igr=20Alex=C3=A1nder=20Fern=C3=A1ndez=20Sa=C3=BAco?= Date: Wed, 1 Apr 2020 11:42:07 -0400 Subject: [PATCH] DockerImagePuller checks the installed images first (#281) * DockerImagePuller checks if the images is already installed before pull it * Use --filter and --format options to filter and format the output docker images command * Optimize usings * Remove unused variable * Improve PullContainerAsync implementation and also throws CommandException if docker images command failed * Improve the approach to determine if it's installed and print the output of this command images in case of failure --- .../DockerImagePuller.cs | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.Tye.Hosting/DockerImagePuller.cs b/src/Microsoft.Tye.Hosting/DockerImagePuller.cs index e88dfdce..3e661260 100644 --- a/src/Microsoft.Tye.Hosting/DockerImagePuller.cs +++ b/src/Microsoft.Tye.Hosting/DockerImagePuller.cs @@ -2,9 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Tye.Hosting.Model; @@ -66,16 +64,33 @@ namespace Microsoft.Tye.Hosting { await Task.Yield(); + var result = await ProcessUtil.RunAsync( + "docker", + $"images --filter \"reference={image}\" --format \"{{{{.ID}}}}\"", + throwOnError: false); + + if (result.ExitCode != 0) + { + _logger.LogInformation("{Image}: " + result.StandardError, image); + throw new CommandException("Docker images command failed"); + } + + if (!string.IsNullOrWhiteSpace(result.StandardOutput)) + { + _logger.LogInformation("Docker image {image} already installed", image); + return; + } + var command = $"pull {image}"; _logger.LogInformation("Running docker command {command}", command); - var result = await ProcessUtil.RunAsync( - "docker", - command, - outputDataReceived: data => _logger.LogInformation("{Image}: " + data, image), - errorDataReceived: data => _logger.LogInformation("{Image}: " + data, image), - throwOnError: false); + result = await ProcessUtil.RunAsync( + "docker", + command, + outputDataReceived: data => _logger.LogInformation("{Image}: " + data, image), + errorDataReceived: data => _logger.LogInformation("{Image}: " + data, image), + throwOnError: false); if (result.ExitCode != 0) {