コード例 #1
0
#pragma warning disable SA1011 // Closing square brackets should be spaced correctly
        public async Task BuildImageAsync(string dockerfile, string imageName, string tag = "latest", string context = ".", IDictionary <string, string>?buildArgs = null, string[]?ignoredFiles = default, CancellationToken cancellationToken = default)
#pragma warning restore SA1011 // Closing square brackets should be spaced correctly
        {
            var tarFileName = Guid.NewGuid().ToString();

            try
            {
                // In order to pass the context we have to create tar file and use it as an argument.
                await _archiver.CreateTarArchiveAsync(tarFileName, context, ignoredFiles, cancellationToken);

                // Now call docker api.
                await CreateNewImageAsync(dockerfile, imageName, tag, tarFileName, buildArgs, cancellationToken);
            }
            catch (Exception exc)
            {
                _logger?.LogError(exc, $"Unable to create the image from dockerfile.");
                throw;
            }
            finally
            {
                // And don't forget to remove created tar.
                try
                {
                    File.Delete(tarFileName);
                }
                catch (Exception exc)
                {
                    _logger?.LogError(exc, $"Unable to delete tar file {tarFileName} with context. Please, cleanup manually.");
                }
            }
        }