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.34k stars 97 forks source link

Ductus.FluentDocker.Common.FluentDockerException : Could not dispose composite service from file(s) #291

Open enrico23 opened 1 year ago

enrico23 commented 1 year ago

I have some integration tests running on Docker using FluentDocker, version 2.10.59

I am using the NUnit lifecycle to create and destroy instances of a Test Environment.

You can see the implementation of the class 'TestEnvironment':

public sealed class SystemTestEnvironment
{
    private SystemTestEnvironment()
    {
        DockerHost = DockerHelpers.GetDockerHost();
        DockerComposeService = Build(DockerHost);
        try
        {
            DockerComposeService.Start();
        }
        catch(Exception ex)
        {
            Debug.WriteLine($"[Fluent Docker Error]: {ex} ");
            DockerComposeService.Dispose();
            throw;
        }
        OnContainerInitialized();
    }

    // ...  code removed  for simplicity

    public static void Initialize()
    {
        Instance = new SystemTestEnvironment();
    }

        // ...  code removed  for simplicity

     private ICompositeService Build(IHostService dockerHost)
    {
        return new DockerComposeCompositeService(dockerHost, new DockerComposeConfig
        {
            ComposeFilePath = new List<string> { SqlServerComposeFile },
            ForceRecreate = true,
            RemoveOrphans = true,
            StopOnDispose = true,
            KeepVolumes = false
        });
    }
}

This is the method GetDockerHost

public static IHostService GetDockerHost()
    {
        var hosts = new Hosts().Discover();
        var defaultHost = hosts.FirstOrDefault(x => x.IsNative) ?? hosts.FirstOrDefault(x => x.Name == "default");
        var host = defaultHost ?? (hosts.Count > 0 ? hosts.First() : GetDockerHost());
        if (host.State is not ServiceRunningState.Running)
        {
            host.Start();
        }

        return host;
    }

I am getting the error when I am attempting

 DockerComposeService.Dispose();

The code was working previously on Azure DevOps and still works locally.

I use GitHub Actions and workflow for CI

The Github agent has the following Docker Tooling installed: Docker version 20.10.24+azure-1 docker-compose version 1.29.2

mariotoffia commented 1 year ago

Hi @enrico23 and thanks for the bug report. I do not have access to an azure environment. Have you tried to enable logging and see what commands being emitted and tried them manually. If not, please do and add the commands and which of them were erroneous (if any)?

Cheers, Mario :)

enrico23 commented 1 year ago

Hi @mariotoffia. Thanks for looking into it.

I found the issue!

I added the "project name" for the Docker Compose file to determine how it was displayed by Docker Desktop.

version: "3.8"
name: "sqltest1" ########### this line caused the issue ######
services:
    sql-server-db:
        hostname: sqltest1
        container_name: sqltest1-container
        image: mssqltest1
        build:
           context: mssql
           dockerfile: Dockerfile
        ports:
           - "1433:1433"
        environment:
          ACCEPT_EULA: Y
          SA_PASSWORD: ****
          MSSQL_PORT:  1433
          MSSQL_DB: ******
          MSSQL_USER: ******
          MSSQL_PASSWORD: ******
        volumes:
        - ./database/msqqldata:/var/opt/mssql/data
        networks:
        - servicetest1-network
networks:
  servicetest1-network:
    driver: bridge
theodinspire commented 10 months ago

We're running into a similar item in our Ubuntu build environment. The environment has Docker Compose versions v1 1.29.2 and v2 2.23.3. It looks like docker-compose which is what FluentDocker uses points to v1, but our compose file is set up for v2.

We're currently adding a build step that aliases docker-compose to docker compose