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

Why does WithDockerfileDirectory() when called will look for the directory in bin/Debug/net... #558

Closed
haridasnykiel opened this issue Aug 9, 2022 · 14 comments
Labels
enhancement New feature or request
Milestone

Comments

@haridasnykiel
Copy link

Why does the following method WithDockerfileDirectory() called from the ImageFromDockerfileBuilder class look at the bin folder by default? I would of expected it to look at the relative path of the class it is being called from.

Also I think the docs around the build with dockerfile is a bit lacking as it took me a while to figure out how to build the image from a docker file.

Currently using version <PackageReference Include="Testcontainers" Version="2.1.0" />

@haridasnykiel haridasnykiel changed the title Why does WithDockerfileDirectory() when called will look for the directory in the bin/Debug/net Why does WithDockerfileDirectory() when called will look for the directory in bin/Debug/net... Aug 9, 2022
@haridasnykiel
Copy link
Author

haridasnykiel commented Aug 9, 2022

Thanks in advance

@ChrisTTian667
Copy link

Did you find a solution?

Since a couple of hours, I'm trying hard to build a docker image from a relative path. With no success.

@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Aug 10, 2022

@haridasnykiel

Why does the following method WithDockerfileDirectory() called from the ImageFromDockerfileBuilder class look at the bin folder by default?

The base path (current working directory) is the location of the executable (assembly). All kinds of configuration files are copied to the output directory (native support by MSBuild and dotnet), I think it is common. Even the access of the build output is much more straightforward.

I would of expected it to look at the relative path of the class it is being called from.

Why is that?

@ChrisTTian667

Since a couple of hours, I'm trying hard to build a docker image from a relative path. With no success.

Can you add a small example that you are trying to achieve?

@HofmeisterAn HofmeisterAn added the question Have you tried our Slack workspace (https://testcontainers.slack.com)? label Aug 10, 2022
@haridasnykiel
Copy link
Author

haridasnykiel commented Aug 10, 2022

Yea I understand that, but it is not common to copy the dockerfile to the executable directory. Therefore I think it would be good to have this point to the root of the project by default or the folder of the class where the call is made.

I would be happy to contribute to make this change?

@HofmeisterAn
Copy link
Collaborator

I would be happy to contribute to make this change?

That would be great. Do you have anything specific in mind? I was thinking about an overloaded method, maybe something like this:

public readonly struct CommonDirectoryPath
{
  // TODO: Detect common directory paths
  public static readonly CommonDirectoryPath GitRoot = new CommonDirectoryPath("");

  public static readonly CommonDirectoryPath ProjectRoot = new CommonDirectoryPath("");

  private CommonDirectoryPath(string directoryPath)
  {
    this.DirectoryPath = directoryPath;
  }

  public string DirectoryPath { get; }
}

[...]

public IImageFromDockerfileBuilder WithDockerfileDirectory(CommonDirectoryPath directoryPath, string dockerfileDirectory)
{
  return this.WithDockerfileDirectory(Path.Combine(directoryPath.DirectoryPath, dockerfileDirectory));
}

@ChrisTTian667
Copy link

ChrisTTian667 commented Aug 10, 2022

@HofmeisterAn , i've a solution with 2 projects

Solution

  • TestService.Api
    • SomeCode.cs
    • DockerFile
  • TestService.Api.Tests
    • IntegrationTest.cs

From the IntegrationTest in TestService.Api.Tests i try to create the TestService Docker Image like

        var apiContainerImageBuilder = new ImageFromDockerfileBuilder()
            .WithName("testservice")
            .WithDeleteIfExists(true)
            .WithDockerfileDirectory("../../../../TestService.Api")
            .WithCleanUp(true);

        var apiContainerImage = await apiContainerImageBuilder.Build();

But this results in an InvalidOperationException saying Docker image testserice:latest has not been created. It seems to be a problem with the folder, but i don't get the problem here. I also tried to specify the DockerFile and the Path as an absolute path, but with the same result.

Can you lead me into the right direction?

@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Aug 10, 2022

Can you lead me into the right direction?

It is Dockerfile not DockerFile (F is lowercase). Or use .WithDockerfile("DockerFile").

@ChrisTTian667
Copy link

It is Dockerfile not DockerFile (F is lowercase). Or use .WithDockerfile("DockerFile").

Sorry, it was just wrong in my description. It is called Dockerfile in the solution.

I've pushed the Sample project into a public repo here:
https://github.com/ChrisTTian667/TestContainersSample

@HofmeisterAn
Copy link
Collaborator

Remove line 18 in your .Dockerignore file (**/Dockerfile*). Testcontainers creates a tar file that contains all necessary files (it tars the base directory).

var dockerfileArchiveFilePath = dockerfileArchive.Tar();

**/Dockerfile* excludes the Dockerfile from the archive.

@ChrisTTian667
Copy link

Remove line 18 in your .Dockerignore file (**/Dockerfile*). Testcontainers creates a tar file that contains all necessary files (it tars the base directory).

var dockerfileArchiveFilePath = dockerfileArchive.Tar();

**/Dockerfile* excludes the Dockerfile from the archive.

Thank you so much. This line costs me half a day. That works perfect :)

@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Aug 10, 2022

Thank you so much. This line costs me half a day. That works perfect :)

You're welcome. Yeah, is not easy to discover. Unfortunately, the Docker remote API doesn't throw any error. We could check for mandatory files, though.

@haridasnykiel
Copy link
Author

I have not yet had a think about it, ill look into it later today. Your solution looks good though ill give it a go. Also have not had a chance to properly look at the code base yet, so going to need sometime to familarise myself with it.

@haridasnykiel
Copy link
Author

Sorry for the delay, been quite busy recently. So I have solution for this now, using the interface you suggested above. I want to test as a nuget package. How can I go creating the nuget package, its in the bin directory as I was expecting. I tried using the cake task, but getting an error:

Error: Could not reach target 'Create-NuGet-Packages' since it was skipped due to a criteria.

Never used cake before.

@HofmeisterAn
Copy link
Collaborator

The task is designed to run on CI/CD only. To bypass this change:

.WithCriteria(() => param.ShouldPublish)

to .WithCriteria(() => param.ShouldPublish || true).

@HofmeisterAn HofmeisterAn added enhancement New feature or request and removed question Have you tried our Slack workspace (https://testcontainers.slack.com)? labels Sep 4, 2022
@HofmeisterAn HofmeisterAn added this to the 2.2.0 milestone Sep 4, 2022
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

3 participants