Skip to content

Commit

Permalink
feat: Ignore FROM args while pre pulling images
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Oct 5, 2023
1 parent 86b8258 commit 2ba02ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/Testcontainers/Images/DockerfileArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ public IEnumerable<IImage> GetBaseImages()
.Where(line => !line.StartsWith("#", StringComparison.Ordinal))
.Select(line => FromLinePattern.Match(line))
.Where(match => match.Success)
// Until now, we are unable to resolve variables within Dockerfiles. Ignore base
// images that utilize variables. Expect them to exist on the host.
.Where(match => !match.Groups[imageGroup].Value.Contains('$'))
.Where(match => !match.Groups[imageGroup].Value.Any(char.IsUpper))
.ToArray();

var stages = lines
Expand All @@ -105,7 +101,9 @@ public IEnumerable<IImage> GetBaseImages()

var images = lines
.Select(match => match.Groups[imageGroup])
.Select(group => group.Value)
.Select(match => match.Value)
.Where(line => !line.Contains('$'))
.Where(line => !line.Any(char.IsUpper))
.Where(value => !stages.Contains(value))
.Distinct()
.Select(value => new DockerImage(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public void DockerfileArchiveGetBaseImages()
// Given
IImage image = new DockerImage("localhost/testcontainers", Guid.NewGuid().ToString("D"), string.Empty);

var dockerfileArchive = new DockerfileArchive("Assets//pullBaseImages/", "Dockerfile", image, NullLogger.Instance);
var dockerfileArchive = new DockerfileArchive("Assets/pullBaseImages/", "Dockerfile", image, NullLogger.Instance);

// When
var baseImages = dockerfileArchive.GetBaseImages();
var baseImages = dockerfileArchive.GetBaseImages().ToArray();

// Then
Assert.Equal(3, baseImages.Count());
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));
}

[Fact]
Expand Down

0 comments on commit 2ba02ef

Please sign in to comment.