Skip to content

Cleanup on error

Compare
Choose a tag to compare
@mariotoffia mariotoffia released this 15 Oct 08:52
· 386 commits to master since this release

This release targets cleanup when errors do occur. It also includes small features such as privileged containers.

A new static type has been introduced, it is called Fd and it can be used to build, discover hosts and run single container or multiple containers. This will deprecate new Builder() and new Hosts() and will be completely removed in 3.0.0. Switch over to use this Fd class (in namespace Ductus.FluentDocker).

When an application terminates with a unhandled exception, .net will not always call finally, therefore if you count to have containers to be cleaned up within a using may be up for a surprise. This depends. To be completely sure it is possible to use e.g.

     // Use Fd.Run for generic cases and Fd.Composite for compose containers

      Fd.Container(c => c.UseContainer()
          .UseImage("postgres:9.6-alpine")
          .ExposePort(5432)
          .WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
          .WaitForPort("5432/tcp", TimeSpan.FromSeconds(30)),
        svc =>
        {
          var config = svc.GetConfiguration(); // a running container here
        });

This can be useful when you may expect an unhandled exception to be thrown after the container is started and before the container is disposed. However, if you have other configuration than Fail Fast or do have a exception handler, you will not need this!

However, nowadays, if the hooks fails such as WaitForPort it will make sure to Dispose the instance and therefore stop and delete any underlying container (if not configured to be persisted). This is a change, earlier they did not invoke Dispose this was up to the invoker to do such.

      using (var svc = Fd
                        .UseContainer()
                        .UseCompose()
                        .FromFile(file)
                        .RemoveOrphans()
                        .WaitForHttp("wordpress", "http://localhost:8000/wp-admin/install.php") 
                        .Build().Start())
      {
        var installPage = await "http://localhost:8000/wp-admin/install.php".Wget(); // running service here
     }

Sample Builder usage.

Cheers,
Mario