Skip to content

Commit

Permalink
chore: Do not pre-pull cached images (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Oct 21, 2023
1 parent aa4385d commit 082eea4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Testcontainers.FakeGcsServer/FakeGcsServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Testcontainers.FakeGcsServer;
[PublicAPI]
public sealed class FakeGcsServerBuilder : ContainerBuilder<FakeGcsServerBuilder, FakeGcsServerContainer, FakeGcsServerConfiguration>
{
public const string FakeGcsServerImage = "fsouza/fake-gcs-server:1.47.5";
public const string FakeGcsServerImage = "fsouza/fake-gcs-server:1.47";

public const ushort FakeGcsServerPort = 4443;

Expand Down
3 changes: 2 additions & 1 deletion src/Testcontainers/Clients/FilterByProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ namespace DotNet.Testcontainers.Clients

internal sealed class FilterByProperty : ConcurrentDictionary<string, IDictionary<string, bool>>
{
public void Add(string property, string value)
public FilterByProperty Add(string property, string value)
{
var values = GetOrAdd(property, _ => new Dictionary<string, bool>());
values[value] = true;
return this;
}
}
}
13 changes: 12 additions & 1 deletion src/Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,18 @@ public async Task<string> BuildAsync(IImageFromDockerfileConfiguration configura
{
var dockerfileArchive = new DockerfileArchive(configuration.DockerfileDirectory, configuration.Dockerfile, configuration.Image, _logger);

await Task.WhenAll(dockerfileArchive.GetBaseImages().Select(image => PullImageAsync(image, ct)))
var baseImages = dockerfileArchive.GetBaseImages().ToArray();

var filters = baseImages.Aggregate(new FilterByProperty(), (dictionary, baseImage) => dictionary.Add("reference", baseImage.FullName));

var cachedImages = await Image.GetAllAsync(filters, ct)
.ConfigureAwait(false);

var repositoryTags = new HashSet<string>(cachedImages.SelectMany(image => image.RepoTags));

var uncachedImages = baseImages.Where(baseImage => !repositoryTags.Contains(baseImage.FullName));

await Task.WhenAll(uncachedImages.Select(image => PullImageAsync(image, ct)))
.ConfigureAwait(false);

_ = await Image.BuildAsync(configuration, dockerfileArchive, ct)
Expand Down
10 changes: 8 additions & 2 deletions tests/Testcontainers.Tests/Assets/pullBaseImages/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime
FROM build
FROM build AS publish
FROM mcr.microsoft.com/dotnet/aspnet:6.0.21-jammy-amd64
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:6.0.22-jammy-amd64
FROM mcr.microsoft.com/dotnet/aspnet:6.0.22-jammy-amd64

# https://github.com/testcontainers/testcontainers-dotnet/issues/993.
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:6.0.23-jammy-amd64

# https://github.com/testcontainers/testcontainers-dotnet/issues/1030.
FROM mcr.microsoft.com/dotnet/sdk:$SDK_VERSION_6_0 AS build_sdk_6_0
FROM build_sdk_6_0 AS publish_sdk_6_0
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void DockerfileArchiveGetBaseImages()
Assert.Equal(4, baseImages.Length);
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/sdk:6.0".Equals(item.FullName));
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/runtime:6.0".Equals(item.FullName));
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/aspnet:6.0.21-jammy-amd64".Equals(item.FullName));
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/aspnet:6.0.22-jammy-amd64".Equals(item.FullName));
Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/aspnet:6.0.23-jammy-amd64".Equals(item.FullName));
}

[Fact]
Expand Down

0 comments on commit 082eea4

Please sign in to comment.