Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Do not pre-pull cached images #1032

Merged
merged 2 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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