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

[Enhancement]: Resolve variables (ARGs) during Docker image build #980

Open
HofmeisterAn opened this issue Aug 19, 2023 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Aug 19, 2023

Problem

Testcontainers for .NET cannot resolve variables (ARGs) during the Docker image build process. Currently, Testcontainers does not pull base images in advance that utilize variables. Hence, if the images are not already present on the host, they will not be pulled, causing the Docker image build process to fail.

Solution

To pull base images that utilize variables in advance too, Testcontainers needs to have the capability to resolve the default values and build arguments of variables.

Benefit

Developers will not be required to manually pull the images beforehand, enabling a more streamlined Docker image building and testing configuration.

Alternatives

Pull images manually beforehand.

Would you like to help contributing this enhancement?

Yes

@HofmeisterAn
Copy link
Collaborator Author

HofmeisterAn commented Sep 3, 2023

Resolving variables (ARGs) should be done here (the matched lines contains already an arg Regex group):

/// <summary>
/// Gets a collection of base images.
/// </summary>
/// <remarks>
/// This method reads the Dockerfile and collects a list of base images. It
/// excludes stages that do not correspond to base images. For example, it will not include
/// the second line from the following Dockerfile configuration:
/// <code>
/// FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
/// FROM build
/// </code>
/// </remarks>
/// <returns>An <see cref="IEnumerable{T}" /> of <see cref="IImage" />.</returns>
public IEnumerable<IImage> GetBaseImages()
{
const string imageGroup = "image";
var lines = File.ReadAllLines(Path.Combine(_dockerfileDirectory.FullName, _dockerfile.ToString()))
.Select(line => line.Trim())
.Where(line => !string.IsNullOrEmpty(line))
.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
.Select(line => line.Value)
.Select(line => line.Split(new [] { " AS ", " As ", " aS ", " as " }, StringSplitOptions.RemoveEmptyEntries))
.Where(substrings => substrings.Length > 1)
.Select(substrings => substrings[substrings.Length - 1])
.Distinct()
.ToArray();
var images = lines
.Select(match => match.Groups[imageGroup])
.Select(group => group.Value)
.Where(value => !stages.Contains(value))
.Distinct()
.Select(value => new DockerImage(value))
.ToArray();
return images;
}

@samba2
Copy link

samba2 commented Apr 22, 2024

I am missing the parsing of "ARG" as well. I use Testcontainers to smoke test a rather complicated Dockerfile which is configured by builds-args/ ARGS. At the moment I manually maintain a second Dockerfile just for the Testcontainers E2E test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants