testcontainers / testcontainers-dotnet

A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.

Home Page:https://dotnet.testcontainers.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Enhancement]: Support specifying Dockerfile path separate to build context

jwm01cg opened this issue · comments

Problem

Hi, I'm loving this library so far and have been able to use it to great effect with supporting services like databases.

I was looking into using it to test our app inside a container, but I ran into a problem - our monorepo's directory structure doesn't appear to be compatible with the image builder.

Our project structure looks like this:

Repo
├──App.sln
├──nuget.config
├──Directory.Build.props
├──Project1
│  └──Dockerfile
│  └──Project1.API
│     └──Project1.API.csproj
│  └──Project1.Tests
│     └──Project1.Tests.csproj
├──Project2
│  └──Dockerfile
│  └──Project2.API
│     └──Project2.API.csproj

Hopefully this badly hand-made tree diagram gets the point across, our actual folder is much bigger so I thought I'd just give an example. Anyway, when we want to build our docker images we typically run this command from the repository root:

docker build -f Project1/Dockerfile .

Which will use the current directory as the build context, so that the Dockerfile can copy solution-wide files like nuget.config and Directory.Build.props, but will build using the Dockerfile for Project1.

As far as I can see, this is not a supported scenario with the image builder. I tried to do this:

IFutureDockerImage image = new ImageFromDockerfileBuilder()
    .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), string.Empty)
    .WithDockerfile("Project1/Dockerfile")
    .Build();

However, this doesn't appear to work and the image hangs on a build. Looking at the docs it seems WithDockerfile is not really for specifying the path but rather the name of the Dockerfile.

Solution

Allow specifying the Dockerfile path separately to the build context - maybe a WithDockerfilePath and WithBuildContext call on the builder? As long as there is a way to express the above docker build command I will be happy.

Benefit

Allows supporting a wider variety of project structures

Alternatives

  • I could restructure my project to have all my Dockerfiles in my repository root with different names
  • I could create an alternative test-only Dockerfile which can build the image when using the context as the Project1 folder

Would you like to help contributing this enhancement?

Yes

I've been a bit silly, the 'hang' was because our .dockerignore sucked and it was copying the entire repository to the build context. The snippet I posted actually works after a while 🤦