DockerSdk
the .NET SDK for Docker
|
DockerSdk is the .NET SKD for Docker. It's a fully asynchronous, non-blocking way to interact with a Docker daemon programmatically. It works with .NET 5.0 or later.
Be aware that, until version 1.0, the API will be subject to change without notice. (This is consistent with semver.)
DockerSdk is published as a NuGet package. You can install it from within Visual Studio / Visual Studio Code, or from the command line:
Package Manager:
.NET CLI:
Package reference:
F# interactive:
The SDK is available in the DockerSdk
namespace. Connecting to a Docker daemon on the local machine is as easy as:
If you want to use the configuration from the standard environment variables, use:
For more control over the connection, see ClientOptions and the other overloads of StartAsync.
Once you have a Docker client object, you can access its functionality via the Containers
, Images
, etc. properties.
List containers:
Create an image by pulling from a Docker Registry:
Run a container:
Stop a container:
The SDK can work with public and private registries. (A registry is a site that hosts Docker images, such as DockerHub.) If you don't provide credentials for a registry, the SDK will attempt to connect anonymously. For some registries that's sufficient to pull from public repositories. To access private repositories, however, you will need to provide credentials before starting the push/pull operation. This is similar to the Docker CLI's docker login
command.
For registries that authenticate by username and password, use AddBasicAuth. For example, to authenticate with DockerHub you would use:
For registries that authenticate by identity token, use AddIdentityToken. For example, to authenticate with the Azure registry you would use:
When the SDK connects a client to a Docker daemon, it automatically negotiates the API version to use. Higher API versions allow newer features, whereas lower API versions have fewer capabilities. The SDK automatically uses the highest API version that both the SDK and the daemon support. If there is no overlap in support, StartAsync
will throw a DockerVersionException.
If you attempt to use a feature that the negotiated API doesn't support, the SDK will throw a NotSupportedException
. To avoid this, check the client object's ApiVersion property to verify that the negotiated version is suitable for your needs. The documentation for the SDK's methods will tell you which API versions they're supported on. If there is no such comment, you may safely assume that it's supported.
Version support:
SDK versions | API versions | Docker versions |
---|---|---|
0.1.0 - 0.5.0 | 1.41 - 1.41 | 20.10 - 20.10 |
If you find that you need more fine-grained control over the functionality, or if you need better performance than the SDK can give you, you can skip the SDK layer and instead use Docker.DotNet. This may require more expertese with Docker and the conventions of the Docker REST API, but it provides a strong foundation for working with the Docker daemon at the lowest-available level.
DockerSdk is licensed under the MIT license.