testcontainers-dotnet: Can't create docker image for dotnet web api project
Describe the bug I have Web API .NET 6 project with docker file in it, when I try creating docker file using ImageFromDockerFileBuilder it failed with not incomprehensible probelm: “System.AggregateException: ‘One or more errors occurred. (Docker image verte.authorizationserver.webapiapplication:1665577986 has not been created.)’”
It’s does not give more errors so I can’t know what is wrong.
To Reproduce Steps to reproduce the behavior:
- I created a function with the parameters of the Image dockerfile builder as follow:
IDockerImage image = await new ImageFromDockerfileBuilder() .WithName(this) .WithDockerfileDirectory("C:\\Repos\\verte.Microservices\\Applications\\AuthorizationServer\\Application\\Verte.AuthorizationServer.WebApiApplication") .WithDockerfile("Dockerfile") .WithBuildArgument("RESOURCE_REAPER_SESSION_ID", ResourceReaper.DefaultSessionId.ToString("D")) .WithDeleteIfExists(false) .Build() .ConfigureAwait(false); - Build
- Run the test
- See error
Expected behavior Dockerfile should be created, also my dockerfile is working just fine with docker compose and docker CLI.
Screenshots

About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 34
Commits related to this issue
- fix(#610): Do not deny all files in the Docker image tarball when a .dockerignore entry ends with /* — committed to testcontainers/testcontainers-dotnet by HofmeisterAn 2 years ago
- fix(#610): Trim traling slashes in Dockerfile directory path (otherwise, it cuts the first character of the relative path) — committed to testcontainers/testcontainers-dotnet by HofmeisterAn 2 years ago
- fix(#610): Trim traling slashes in Dockerfile directory path (otherwise, it cuts the first character of the relative path), Normalize paths to forward slashes (#651) — committed to testcontainers/testcontainers-dotnet by HofmeisterAn 2 years ago
I will close this issue, because it contains all kinds of information to set up the configuration properly. @Omrisha this might be interesting for you as well: https://github.com/testcontainers/testcontainers-dotnet/issues/610#issuecomment-1299996820. Depending on your Dockerfile this might work:
Thanks to everyone. Due to your various configurations, we were able to add some more improvements to the builder. Version 2.2.0 as well as the next release are not that strict anymore regarding path conventions etc.
If you still struggle, to set it up, please do not hesitate to reopen the issue again.
That is fixed with #604.
Testcontainters for .NET creates a tarball of the content of
WithDockerfileDirectoryand passes the tarball to Docker. This tarball is the “new” root of the Dockerfile and must contain all files, etc.Your configuration above looks good. The
Dockerfileis inside theWithDockerfileDirectorydirectory tree. Does it still not work?Docker.Dotnet does not forward the error. The issue has been fixed recently with https://github.com/dotnet/Docker.DotNet/pull/590, but there is no new release yet. But I agree, we need to improve the developer experience here, it looks like the current implementation or behavior is too complicated.
Does that help?
You exclude your own Dockerfile.
I have effectively not may enough attention on the path you give me, my mistake. Thank you.
Builder configuration
WeatherForecastDockerfile
Alright best I can tell, my
.yarn/*entry in my dockerfile was getting turned into the pattern^([\\\/]?(([^\\\/]+)?$\b|$))which matchedDirectory.Packages.props. Switched it to.yarnand all is well.The logging was because I wasn’t setting the logger early enough in the process. I was building the image in a fixture, but trying to use
ITestOutputHelperwhich doesn’t seem like you can get access to in fixtures.I currently have the same problem. After quite a bit of debugging, I found that trailing
/s in .dockerignore cause all files to get ignored. Docker doesn’t care, but the ignore logic in this lib will turn them into regex’s that match all files, and thus ignore all files. For example,**/bin/vs**/bin. Something to check at the very least.For me, after fixing that it still fails to build the image. Does
WithDockerfileDirectorycorrespond to the build context? ShouldWithDockerfilebe relative to the path given inWithDockerfileDirectory?In my case, my Dockerfile lives at
myrepo/src/common/dotnet/DockerfileAnd the build context is expected to bemyrepo/srcTo build the image, I’m usingI feel like this issue would be pretty easy to solve if there was any inkling of an error message. I tried debugging into the internal Docker.Dotnet calls to see if I could pull anything from the log steam it seems to be using, but either my debugging skills are lacking or there’s nothing there.
I also tried setting up an XUnit
ITestOutputHelperand passing that to Testcontainers, but since the error seems to be happening with Docker.Dotnet that didn’t lead to much.