-
-
Notifications
You must be signed in to change notification settings - Fork 288
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]: $BUILDPLATFORM #993
Comments
This might already work out of the box with the latest changes from the |
I have just testet it against 3.5.0 which i can see is the same as the latest on develop. I tried the following TestcontainersSettings.Logger = LoggerFactory.Create(x =>
{
x.AddConsole();
x.SetMinimumLevel(LogLevel.Debug);
}).CreateLogger<ContainerTest>();
var image = new ImageFromDockerfileBuilder()
.WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), string.Empty)
.WithDockerfile(DockerfilePath)
.WithBuildArgument("BUILDPLATFORM", "linux/arm64")
.Build();
await image.CreateAsync().ConfigureAwait(false); The attached debugger outputs the following, same if i set the WithBuildArgument or not, still fails.
|
Oh, I understand now. The Docker build itself is failing. I initially thought it might be connected to the fix mentioned in the PR, where Testcontainers attempts to pull images in advance. I just tested whether Testcontainers correctly selects the image from the line
As you have mentioned, perhaps the builder can handle this task by generating a temporary Dockerfile next to the original Dockerfile. Using the hash of the original file to determine whether it needs to be created or not. Nevertheless, I am considering whether it might be more straightforward to include an additional Dockerfile, such as "Dockerfile.arm64," within your project. I don't have a strong opinion on this matter; if it helps developers write better tests, a contribution is welcome. I would favor a more generic approach, which may allow more modifications in the future. |
My current solution is to create a temporary file, where i strip the --platform=$BUILDPLATFORM part The --platform=$BUILDPLATFORM is only used when building the image for multiple platforms, and the $BUILDPLATFORM is automatically set by the docker buildkit. I could simple create a file without it, but keeping two files up to date is something that never happens in the long run, so made it happen automatically, but generally, I cant be the only one with this issue. The idea to create this issue was exactly to discuss how to do it, another approach could be to enable one to enable one to pass a var image = new ImageFromDockerfileBuilder()
.WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), string.Empty)
.WithDockerfile(() => File.ReadAllLines("MyDockerfile") )
.Build(); What do you think? |
Yes, I agree. I wouldn't favor maintaining (almost) duplicate configurations either.
I was thinking about something similar. What do you think about an implementation of public class Dockerfile
{
private readonly StringBuilder _stringBuilder = new StringBuilder();
private readonly string _dockerfileFilePath;
public Dockerfile(string dockerfileFilePath)
{
_dockerfileFilePath = dockerfileFilePath;
}
public virtual string Resolve()
{
using (var streamReader = new StreamReader(_dockerfileFilePath))
{
string filePath;
while (!streamReader.EndOfStream)
{
Process(streamReader.ReadLine());
}
using (var sha256 = SHA256.Create())
{
var hashBytes = sha256.ComputeHash(streamReader.CurrentEncoding.GetBytes(_stringBuilder.ToString()));
var hash = BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLower();
filePath = string.Join(".", _dockerfileFilePath, hash.Substring(0, 8));
}
using (var fileStream = new StreamWriter(filePath))
{
fileStream.Write(_stringBuilder.ToString());
}
return filePath;
}
}
protected virtual void Process(string line)
{
_stringBuilder.AppendLine(line);
}
} Then we can call the following line to create the
We can offer a default implementation, and others have the option to override it in order to customize the generation of the |
Problem
I have a dockerfile that contains the following
the
--platform=$BUILDPLATFORM
is only supported by BuildKit as I understand it, which cannot be used over the API currently.This issue make me incapable of running the dockerfile as is with testcontainers.
Solution
Since the solution as now cannot support the functionality through docker, maybe we could include an option to strip these from the Dockerfile in a temporary file, before sending it to the context.
This has is what i ended up doing locally, since I didn't want two files to maintain, and then remove the file once the build is complete, but it ain't the most pretty solution.
My thoughs was maybe to hook in the
DockerfileArchive.tar
method, as to replace the data just before being added to the tar, but just a thought.Benefit
Being able to run testcontainers containing
--platform=$BUILDPLATFORM
with minimal frictionAlternatives
Wait for docker to support buildkit over the api.
Would you like to help contributing this enhancement?
Yes
The text was updated successfully, but these errors were encountered: