mariotoffia / FluentDocker

Use docker, docker-compose local and remote in tests and your .NET core/full framework apps via a FluentAPI
Apache License 2.0
1.31k stars 97 forks source link

Should Composite Container Name be the composer.yml path? #278

Open Fewiel opened 1 year ago

Fewiel commented 1 year ago

I need to set up a container and get the name back to start and stop it later.

Code to create the container:

var svc = new Builder()
    .UseContainer()
    .WithName("docker-container-1")
    .UseCompose().ServiceName("docker-container-1")
    .FromFile("/root/DockerFiles/test.yml")
    .RemoveOrphans()
    .Build().Start();

test.yml:

version: "3"

services:
    client:
        image: nginx
        ports:
            - 80:80
        volumes:
            - ./src:/usr/share/nginx/html

The svc.Name returns "/root/DockerFiles/test.yml"
I expected it to return: docker-container-1
docker container ls returns the correct container name (but with _client_1 added)

Please let me know if this is the correct way to set the name.

mariotoffia commented 1 year ago

Hi @Fewiel - this is by design. It will return a ICompositeService and it's composition is determined by the "/root/DockerFiles/test.yml". Inspect the contained services to get their respective name.

If there are any problems, please let me know.

From README.md

      var file = Path.Combine(Directory.GetCurrentDirectory(),
        (TemplateString) "Resources/ComposeTests/WordPress/docker-compose.yml");

      // @formatter:off
      using (var svc = new Builder()
                        .UseContainer()
                        .UseCompose()
                        .FromFile(file)
                        .RemoveOrphans()
                        .WaitForHttp("wordpress", "http://localhost:8000/wp-admin/install.php") 
                        .Build().Start())
        // @formatter:on
      {
        // We now have a running WordPress with a MySql database        
        var installPage = await "http://localhost:8000/wp-admin/install.php".Wget();

        Assert.IsTrue(installPage.IndexOf("https://wordpress.org/", StringComparison.Ordinal) != -1);
        Assert.AreEqual(1, svc.Hosts.Count); // The host used by compose
        Assert.AreEqual(2, svc.Containers.Count); // We can access each individual container
        Assert.AreEqual(2, svc.Images.Count); // And the images used.
      }