• Stars
    star
    2,164
  • Rank 20,420 (Top 0.5 %)
  • Language
    C#
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐳 .NET (C#) Client Library for Docker API

.NET Client for Docker Remote API

This library allows you to interact with Docker Remote API endpoints in your .NET applications.

It is fully asynchronous, designed to be non-blocking and object-oriented way to interact with your Docker daemon programmatically.

Versioning

Version of this package uses SemVer format: MAJOR.MINOR.PATCH. MINOR segment indicates the Docker Remote API version support. For instance v2.124.0 of this library supports Docker Remote API v1.24. This does not guarantee backwards compatibility as Docker Remote API does not guarantee that either.

MAJOR is reserved for major breaking changes we make to the library itself such as how the calls are made or how authentication is made. PATCH is just for incremental bug fixes or non-breaking feature additions.

Installation

NuGet latest release

You can add this library to your project using NuGet.

Package Manager Console Run the following command in the “Package Manager Console”:

PM> Install-Package Docker.DotNet

Visual Studio Right click to your project in Visual Studio, choose “Manage NuGet Packages” and search for ‘Docker.DotNet’ and click ‘Install’. (see NuGet Gallery.)

.NET Core Command Line Interface Run the following command from your favorite shell or terminal:

dotnet add package Docker.DotNet

Development Builds

If you intend to use development builds of Docker.DotNet and don't want to compile the code yourself you can add the package source below to Visual Studio or your Nuget.Config.

https://ci.appveyor.com/nuget/docker-dotnet-hojfmn6hoed7

Usage

You can initialize the client like the following:

using Docker.DotNet;
DockerClient client = new DockerClientConfiguration(
    new Uri("http://ubuntu-docker.cloudapp.net:4243"))
     .CreateClient();

or to connect to your local Docker for Windows daemon using named pipes or your local Docker for Mac daemon using Unix sockets:

using Docker.DotNet;
DockerClient client = new DockerClientConfiguration()
     .CreateClient();

For a custom endpoint, you can also pass a named pipe or a Unix socket to the DockerClientConfiguration constructor. For example:

// Default Docker Engine on Windows
using Docker.DotNet;
DockerClient client = new DockerClientConfiguration(
    new Uri("npipe://./pipe/docker_engine"))
     .CreateClient();
// Default Docker Engine on Linux
using Docker.DotNet;
DockerClient client = new DockerClientConfiguration(
    new Uri("unix:///var/run/docker.sock"))
     .CreateClient();

Example: List containers

IList<ContainerListResponse> containers = await client.Containers.ListContainersAsync(
	new ContainersListParameters(){
		Limit = 10,
    });

Example: Create an image by pulling from Docker Registry

The code below pulls fedora/memcached image to your Docker instance using your Docker Hub account. You can anonymously download the image as well by passing null instead of AuthConfig object:

await client.Images.CreateImageAsync(
    new ImagesCreateParameters
    {
        FromImage = "fedora/memcached",
        Tag = "alpha",
    },
    new AuthConfig
    {
        Email = "[email protected]",
        Username = "test",
        Password = "pa$$w0rd"
    },
    new Progress<JSONMessage>());

Example: Create a container

The following code will create a new container of the previously fetched image.

await client.Containers.CreateContainerAsync(new CreateContainerParameters()
    {
        Image = "fedora/memcached",
        HostConfig = new HostConfig()
        {
            DNS = new[] { "8.8.8.8", "8.8.4.4" }
        }
    });

Example: Start a container

The following code will start the created container.

await client.Containers.StartContainerAsync(
    "39e3317fd258",
    new ContainerStartParameters()
    );

Example: Stop a container

The following code will stop a running container.

Note: WaitBeforeKillSeconds field is of type uint? which means optional. This code will wait 30 seconds before killing it. If you like to cancel the waiting, you can use the CancellationToken parameter.

var stopped = await client.Containers.StopContainerAsync(
    "39e3317fd258",
    new ContainerStopParameters
    {
        WaitBeforeKillSeconds = 30
    },
    CancellationToken.None);

Example: Dealing with Stream responses

Some Docker API endpoints are designed to return stream responses. For example Monitoring Docker events continuously streams the status in a format like :

{"status":"create","id":"dfdf82bd3881","from":"base:latest","time":1374067924}
{"status":"start","id":"dfdf82bd3881","from":"base:latest","time":1374067924}
{"status":"stop","id":"dfdf82bd3881","from":"base:latest","time":1374067966}
{"status":"destroy","id":"dfdf82bd3881","from":"base:latest","time":1374067970}
...

To obtain this stream you can use:

CancellationTokenSource cancellation = new CancellationTokenSource();
Stream stream = await client.System.MonitorEventsAsync(new ContainerEventsParameters(), new Progress<JSONMessage>(), cancellation.Token);
// Initialize a StreamReader...

You can cancel streaming using the CancellationToken. On the other hand, if you wish to continuously stream, you can simply pass CancellationToken.None.

Example: HTTPS Authentication to Docker

If you are running Docker with TLS (HTTPS), you can authenticate to the Docker instance using the Docker.DotNet.X509 package. You can get this package from NuGet or by running the following command in the “Package Manager Console”:

PM> Install-Package Docker.DotNet.X509

Once you add Docker.DotNet.X509 to your project, use CertificateCredentials type:

var credentials = new CertificateCredentials (new X509Certificate2 ("CertFile", "Password"));
var config = new DockerClientConfiguration("http://ubuntu-docker.cloudapp.net:4243", credentials);
DockerClient client = config.CreateClient();

If you don't want to authenticate you can omit the credentials parameter, which defaults to an AnonymousCredentials instance.

The CertFile in the example above should be a .pfx file (PKCS12 format), if you have .pem formatted certificates which Docker normally uses you can either convert it programmatically or use openssl tool to generate a .pfx:

openssl pkcs12 -export -inkey key.pem -in cert.pem -out key.pfx

(Here, your private key is key.pem, public key is cert.pem and output file is named key.pfx.) This will prompt a password for PFX file and then you can use this PFX file on Windows. If the certificate is self-signed, your application may reject the server certificate, in this case you might want to disable server certificate validation:

//
// There are two options to do this.
//

// You can do this globally for all certificates:
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;

// Or you can do this on a credential by credential basis:
var creds = new CertificateCredentials(...);
creds.ServerCertificateValidationCallback += (o, c, ch, er) => true;

Example: Basic HTTP Authentication to Docker

If the Docker instance is secured with Basic HTTP Authentication, you can use the Docker.DotNet.BasicAuth package. Get this package from NuGet or by running the following command in the “Package Manager Console”:

PM> Install-Package Docker.DotNet.BasicAuth

Once you added Docker.DotNet.BasicAuth to your project, use BasicAuthCredentials type:

var credentials = new BasicAuthCredentials ("YOUR_USERNAME", "YOUR_PASSWORD");
var config = new DockerClientConfiguration("tcp://ubuntu-docker.cloudapp.net:4243", credentials);
DockerClient client = config.CreateClient();

BasicAuthCredentials also accepts SecureString for username and password arguments.

Example: Specifying Remote API Version

By default this client does not specify version number to the API for the requests it makes. However, if you would like to make use of versioning feature of Docker Remote API You can initialize the client like the following.

var config = new DockerClientConfiguration(...);
DockerClient client = config.CreateClient(new Version(1, 16));

Error Handling

Here are typical exceptions thrown from the client library:

  • DockerApiException is thrown when Docker API responds with a non-success result. Subclasses:
    • DockerContainerNotFoundException
    • DockerImageNotFoundException
  • TaskCanceledException is thrown from System.Net.Http.HttpClient library by design. It is not a friendly exception, but it indicates your request has timed out. (default request timeout is 100 seconds.)
    • Long-running methods (e.g. WaitContainerAsync, StopContainerAsync) and methods that return Stream (e.g. CreateImageAsync, GetContainerLogsAsync) have timeout value overridden with infinite timespan by this library.
  • ArgumentNullException is thrown when one of the required parameters are missing/empty.
    • Consider reading the Docker Remote API reference and source code of the corresponding method you are going to use in from this library. This way you can easily find out which parameters are required and their format.

.NET Foundation

Docker.DotNet is a .NET Foundation project.

There are many .NET related projects on GitHub.

  • .NET home repo - links to 100s of .NET projects, from Microsoft and the community.
  • ASP.NET Core home - the best place to start learning about ASP.NET Core.

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct.

General .NET OSS discussions: .NET Foundation Discord

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.dotnetfoundation.org.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

License

Docker.DotNet is licensed under the MIT license.


Copyright (c) .NET Foundation and Contributors

More Repositories

1

aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
C#
33,217
star
2

maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
C#
21,364
star
3

core

Home repository for .NET Core
PowerShell
19,308
star
4

roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
C#
18,414
star
5

corefx

This repo is used for servicing PR's for .NET Core 2.1 and 3.1. Please visit us at https://github.com/dotnet/runtime
17,793
star
6

runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
C#
13,703
star
7

coreclr

CoreCLR is the runtime for .NET Core. It includes the garbage collector, JIT compiler, primitive data types and low-level classes.
12,807
star
8

efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
C#
12,774
star
9

AspNetCore.Docs

Documentation for ASP.NET Core
C#
12,270
star
10

csharplang

The official repo for the design of the C# programming language
C#
10,743
star
11

BenchmarkDotNet

Powerful .NET library for benchmarking
C#
9,929
star
12

orleans

Cloud Native application framework for .NET
C#
9,460
star
13

blazor

Blazor moved to https://github.com/dotnet/aspnetcore
PowerShell
9,348
star
14

machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
C#
8,456
star
15

reactive

The Reactive Extensions for .NET
C#
6,490
star
16

wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
C#
6,346
star
17

tye

Tye is a tool that makes developing, testing, and deploying microservices and distributed applications easier. Project Tye includes a local orchestrator to make developing microservices easier and the ability to deploy microservices to Kubernetes with minimal configuration.
C#
5,309
star
18

msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
C#
5,073
star
19

winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
C#
4,188
star
20

MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
C#
4,070
star
21

machinelearning-samples

Samples for ML.NET, an open source and cross-platform machine learning framework for .NET.
PowerShell
4,061
star
22

dotnet-docker

Docker images for .NET and the .NET Tools.
Dockerfile
4,033
star
23

docs

This repository contains .NET Documentation.
Dockerfile
3,921
star
24

Open-XML-SDK

Open XML SDK by Microsoft
C#
3,862
star
25

fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
F#
3,741
star
26

docfx

Static site generator for .NET API documentation.
C#
3,663
star
27

Silk.NET

The high-speed OpenGL, OpenCL, OpenAL, OpenXR, GLFW, SDL, Vulkan, Assimp, WebGPU, and DirectX bindings library your mother warned you about.
C#
3,639
star
28

cli

The .NET Core command-line (CLI) tools, used for building .NET Core apps and libraries through your development flow (compiling, NuGet package management, running, testing, ...).
3,495
star
29

command-line-api

Command line parsing, invocation, and rendering of terminal output.
C#
3,095
star
30

standard

This repo is building the .NET Standard
3,073
star
31

aspnet-api-versioning

Provides a set of libraries which add service API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core.
C#
2,954
star
32

roslynator

Roslynator is a set of code analysis tools for C#, powered by Roslyn.
C#
2,913
star
33

corert

This repo contains CoreRT, an experimental .NET Core runtime optimized for AOT (ahead of time compilation) scenarios, with the accompanying compiler toolchain.
C#
2,910
star
34

samples

Sample code referenced by the .NET documentation
C#
2,896
star
35

vscode-csharp

Official C# support for Visual Studio Code
TypeScript
2,806
star
36

try

Try .NET provides developers and content authors with tools to create interactive experiences.
TypeScript
2,806
star
37

interactive

.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
C#
2,732
star
38

sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
C#
2,516
star
39

extensions

This repository contains a suite of libraries that provide facilities commonly needed when creating production-ready applications.
C#
2,361
star
40

maui-samples

Samples for .NET Multi-Platform App UI (.NET MAUI)
C#
2,219
star
41

pinvoke

A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS.
C#
2,079
star
42

spark

.NET for Apache® Spark™ makes Apache Spark™ easily accessible to .NET developers.
C#
1,993
star
43

iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
C#
1,932
star
44

format

Home for the dotnet-format command
C#
1,736
star
45

wcf

This repo contains the client-oriented WCF libraries that enable applications built on .NET Core to communicate with WCF services.
C#
1,664
star
46

Comet

Comet is an MVU UIToolkit written in C#
C#
1,623
star
47

templating

This repo contains the Template Engine which is used by dotnet new
C#
1,536
star
48

roslyn-analyzers

C#
1,515
star
49

llilc

This repo contains LLILC, an LLVM based compiler for .NET Core. It includes a set of cross-platform .NET code generation tools that enables compilation of MSIL byte code to LLVM supported platforms.
C++
1,512
star
50

infer

Infer.NET is a framework for running Bayesian inference in graphical models
C#
1,500
star
51

dotNext

Next generation API for .NET
C#
1,485
star
52

EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
PowerShell
1,477
star
53

corefxlab

This repo is for experimentation and exploring new ideas that may or may not make it into the main corefx repo.
C#
1,462
star
54

ef6

This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.
C#
1,400
star
55

installer

.NET SDK Installer
C#
1,261
star
56

codeformatter

Tool that uses Roslyn to automatically rewrite the source to follow our coding styles
C#
1,235
star
57

ResXResourceManager

Manage localization of all ResX-Based resources in one central place.
C#
1,235
star
58

announcements

Subscribe to this repo to be notified of Announcements and changes in .NET Core.
1,231
star
59

Nerdbank.GitVersioning

Stamp your assemblies, packages and more with a unique version generated from a single, simple version.json file and include git commit IDs for non-official builds.
C#
1,223
star
60

MobileBlazorBindings

Experimental Mobile Blazor Bindings - Build native and hybrid mobile apps with Blazor
C#
1,189
star
61

runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
1,181
star
62

ILMerge

ILMerge is a static linker for .NET Assemblies.
C#
1,175
star
63

try-convert

Helping .NET developers port their projects to .NET Core!
C#
1,138
star
64

sourcelink

Source Link enables a great source debugging experience for your users, by adding source control metadata to your built assets
C#
1,136
star
65

diagnostics

This repository contains the source code for various .NET Core runtime diagnostic tools and documents.
C++
1,092
star
66

upgrade-assistant

A tool to assist developers in upgrading .NET Framework applications to .NET 6 and beyond
C#
982
star
67

project-system

The .NET Project System for Visual Studio
C#
945
star
68

try-samples

C#
920
star
69

TorchSharp

A .NET library that provides access to the library that powers PyTorch.
C#
891
star
70

designs

This repo is used for reviewing new .NET designs.
C#
843
star
71

ClangSharp

Clang bindings for .NET written in C#
C#
840
star
72

crank

Benchmarking infrastructure for applications
C#
819
star
73

LLVMSharp

LLVM bindings for .NET Standard written in C# using ClangSharp
C#
805
star
74

DataGridExtensions

Modular extensions for the WPF DataGrid control
C#
754
star
75

SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
C#
728
star
76

intro-to-dotnet-web-dev

Get Started as a Web Developer with .NET, C#, and ASP.NET Core
C#
666
star
77

Microsoft.Maui.Graphics

An experimental cross-platform native graphics library.
C#
657
star
78

HttpRepl

The HTTP Read-Eval-Print Loop (REPL) is a lightweight, cross-platform command-line tool that's supported everywhere .NET Core is supported and is used for making HTTP requests to test ASP.NET Core web APIs and view their results.
C#
651
star
79

arcade

Tools that provide common build infrastructure for multiple .NET Foundation projects.
C#
642
star
80

csharp-notebooks

Get started learning C# with C# notebooks powered by .NET Interactive and VS Code.
Jupyter Notebook
629
star
81

performance

This repo contains benchmarks used for testing the performance of all .NET Runtimes
F#
620
star
82

Microsoft.Maui.Graphics.Controls

Experimental Microsoft.Maui.Graphics.Controls - Build drawn controls (Cupertino, Fluent and Material)
C#
608
star
83

Scaffolding

Code generators to speed up development.
C#
596
star
84

csharpstandard

Working space for ECMA-TC49-TG2, the C# standard committee.
C#
596
star
85

dotnet-console-games

Game examples implemented as .NET console applications primarily for providing education and inspiration. :)
C#
569
star
86

cli-lab

A guided tool will be provided to enable the controlled clean up of a system such that only the desired versions of the Runtime and SDKs remain.
C#
563
star
87

dotnet-api-docs

.NET API reference documentation (.NET 5+, .NET Core, .NET Framework)
C#
558
star
88

dotnet-docker-samples

The .NET Core Docker samples have moved to https://github.com/dotnet/dotnet-docker/tree/master/samples
C#
545
star
89

WatsonTcp

WatsonTcp is the easiest way to build TCP-based clients and servers in C#.
C#
536
star
90

dotnet-monitor

This repository contains the source code for .NET Monitor - a tool that allows you to gather diagnostic data from running applications using HTTP endpoints
C#
527
star
91

Nerdbank.Streams

Specialized .NET Streams and pipes for full duplex in-proc communication, web sockets, and multiplexing
C#
514
star
92

Kerberos.NET

A Kerberos implementation built entirely in managed code.
C#
490
star
93

blazor-samples

HTML
483
star
94

buildtools

Build tools that are necessary for building the .NET Core projects
479
star
95

roslyn-sdk

Roslyn-SDK templates and Syntax Visualizer
C#
470
star
96

core-setup

Installer packages for the .NET Core runtime and libraries
455
star
97

training-tutorials

Getting started tutorials for C# and ASP.NET
C#
401
star
98

razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
C#
390
star
99

linker

C#
380
star
100

sign

Code Signing CLI tool supporting Authenticode, NuGet, VSIX, and ClickOnce
C#
374
star