Dockert 0.2.1

dotnet add package Dockert --version 0.2.1
                    
NuGet\Install-Package Dockert -Version 0.2.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dockert" Version="0.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dockert" Version="0.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Dockert" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Dockert --version 0.2.1
                    
#r "nuget: Dockert, 0.2.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Dockert@0.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Dockert&version=0.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Dockert&version=0.2.1
                    
Install as a Cake Tool

Dockert 🐳🍵

Dockert 🐳🍵 makes utilizing Docker containers from .NET simpler.

Need a database? Dockert will help you easily test that database, across all platforms.

Have an unwieldy third party dependency? Containerize it!

Doing micro services in the cloud? Dockert feels you. Dockert hears you.

Dockert is here to make it simpler. Because more whales means more tea. Or something.

What is Dockert?

Dockert is a simple wrapper around the Docker APIs and is very useful for creating (more) declarative tests that utilize Docker containers. Dockert utilizes Docker.Dotnet and provides simplifying wrappers as well as a handy, asynchronously disposable container wrapper class which makes writing tests that spin up containers a breeze.

Getting started

Here's how you start a container in a test:

// We use an instance of DockerClient from Docker.DotNet to pull an image, create and start a container
var containerId = await dockerClient.CreateContainer(imageName, environmentVariables, portBindings);
await dockerClient.StartContainer(containerId);

Now you write your arrange/act/asserts as usual, and then when you're done, just:

await dockerClient.StopAndRemoveContainer(containerId);

Since tests may fail and there's boilerplate involved with handling cleanup easily, we also include the AsyncDisposableContainer for you to simplify even further:

await using var container = await AsyncDisposableContainer.FromImage(imageName);

You now have a running container and can arrange/act/assert as normal. The container will be stopped and removed when the test is done, or fails, or something unexpected happens.

You can also subclass AsyncDisposableContainer and create a custom container with a simple API for your particular use case. We recommend using a factory method to instantiate your container, utilizing the Create factory method provided:

public class MysqlContainer : AsyncDisposableContainer
{
    private MysqlContainer(IDockerClient dockerClient, string containerId)
      : base(dockerClient, containerId)
    {   
    }

    public static async Task<MySqlContainer> WithDatabase(string name)
    {
        var container = Create<MySqlContainer>("mysql:latest", 
            new [] 
            { 
                "MYSQL_ROOT_PASSWORD=MyV3ryS3cretP4ssword!", 
                $"MYSQL_DATABASE={name}"
            },
            (dockerClient, containerId) => new MysqlContainer(dockerClient, containerId));
    }
}

Handling lingering containers

Even if we try our best, unexpected errors may occur. Sometimes containers can't be started, or will start, but then exit, or similar, leaving us with containers Dockert can't clean up properly. To remove these (as best we can), just go to your command line and remove them one by one, or simply:

    await dockerClient.PruneContainers(includeRunning: true);

You can also find all containers created by Dockert yourself by listing containers labeled "dockert=container".

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.1 23,676 2/7/2022
0.2.0 1,439 2/7/2022
0.1.0 570 2/2/2022