Skip to content

[Enhancement]: Allow the Docker Image Build Context Path to Be Different From the Dockerfile Path #1514

@phil-bohnenkamp

Description

@phil-bohnenkamp

Problem

When building a Docker image, the build context is directly tied to the location of the Dockerfile. This limitation makes the .NET version of Testcontainers incompatible with tools like docker-compose and other language versions of Testcontainers.

For example, my Git repository structure happens to be:

/
├── .git
├── ClientServiceApi/
    ├── Application/
        ├── ...code file structure
        └── Application.csproj
    ├── Domain/
        ├── ...code file structure
        └── Domain.csproj
    ├── Infrastructure/
        ├── ...code file structure
        └── Infrastructure.csproj
    ├── Grpc.Api/
        ├── ...code file structure
        ├── .dockerignore
        ├── Dockerfile
        └── Grpc.Api.csproj
    ├── Rest.Api/
        ├── ...code file structure
        ├── .dockerignore
        ├── Dockerfile
        └── Rest.Api.csproj
    └── ClientServiceApi.sln
├── SharedDbContext/
    ├── ... code
    ├── SharedDbContext.sln
    ├── DbContext.csproj
├── IntegrationServiceApi/
    ├── ...
├── ...OtherServices/
└── compose.yaml

My Grpc.Api project's Dockerfile needs to copy the projects from the repository root like so:

# Copy all non-test source code
COPY ["./SharedDbContext, "./SharedDbContext"]
COPY ["./ClientServiceApi/ClientServiceApi.sln", "./ClientServiceApi/ClientServiceApi.sln"]
COPY ["./ClientServiceApi/Domain", "./ClientServiceApi/Domain"]
COPY ["./ClientServiceApi/Application", "./ClientServiceApi/Application"]
COPY ["./ClientServiceApi/Infrastructure", "./ClientServiceApi/Infrastructure"]
COPY ["./ClientServiceApi/Grpc.Api", "./ClientServiceApi/Grpc.Api"]

As far as I have been able to determine, Testcontainers-Dotnet cannot build an image from a file structure like this. In my compost.yaml file I am able to set the build context separate from the Dockerfile:

client-grpc-api:
    image: client-grpc-api
    build:
      context: .
      dockerfile: ./ClientServiceApi/Grpc.Api/Dockerfile

It also looks like the testcontainers-python and testcontainers-java versions also allow for this separation.

Solution

You already have a WithDockerfileDirectory(CommonDirectoryPath.GetGitDirectory(), string.Empty) method. Would you be able to use the first argument as the context and the second argument as the path to the dockerfile?

I think the optimal solution would be to make the image build context a first class concept like the testcontainers-python and testcontainers-java do but I don't see how to do so without breaking backwards compatibility offhand.

Benefit

Arguments about the unconventional nature of this repository structure aside, sometimes the real world is messy like this. I could see monorepos having this problem. This enhancement would allow for larger and unconventional solution/project structures to use Testcontainers-dotnet without some ugly workarounds.

Alternatives

To work around this issue, I am creating a directory and file structure that is compatible with both my Dockerfile and Testcontainers-dotnet in a temp directory before I create the image. This makes my already long running integration tests longer.

Would you like to help contributing this enhancement?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions