### Config C# Model for JSON Body Source: https://github.com/dotnet/docker.dotnet/blob/master/tools/specgen/README.md Example of a generated C# model for a configuration object, illustrating how fields are mapped for JSON serialization using DataMember attributes. It shows how default values are handled. ```csharp using System.Collections.Generic; using System.Runtime.Serialization; namespace Docker.DotNet.Models { [DataContract] public class Config // (container.Config) { [DataMember(Name = "Hostname", EmitDefaultValue = false)] public string Hostname { get; set; } [DataMember(Name = "Domainname", EmitDefaultValue = false)] public string Domainname { get; set; } // etc... } } ``` -------------------------------- ### Start Docker Container Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Starts a previously created Docker container using its ID. This is an asynchronous operation. ```csharp await client.Containers.StartContainerAsync( "39e3317fd258", new ContainerStartParameters() ); ``` -------------------------------- ### Container Attach Parameters C# Model Source: https://github.com/dotnet/docker.dotnet/blob/master/tools/specgen/README.md Example of a generated C# model for container attach parameters. It demonstrates how query string parameters like 'stream' and 'stdin' are defined with their respective types and attributes. ```csharp using System.Runtime.Serialization; namespace Docker.DotNet.Models { [DataContract] public class ContainerAttachParameters // (main.ContainerAttachParameters) { [QueryStringParameter("stream", false, typeof(BoolQueryStringConverter))] public bool? Stream { get; set; } [QueryStringParameter("stdin", false, typeof(BoolQueryStringConverter))] public bool? Stdin { get; set; } } } ``` -------------------------------- ### HTTPS Authentication to Docker with X.509 Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Explains how to authenticate to a Docker instance secured with HTTPS using X.509 certificates. It details installing the `Docker.DotNet.X509` package and using `CertificateCredentials`. It also covers handling certificate validation callbacks and converting PEM to PFX format. ```bash PM> Install-Package Docker.DotNet.X509 ``` ```csharp var credentials = new CertificateCredentials (new X509Certificate2 ("CertFile", "Password")); var config = new DockerClientConfiguration("http://ubuntu-docker.cloudapp.net:4243", credentials); DockerClient client = config.CreateClient(); ``` ```bash openssl pkcs12 -export -inkey key.pem -in cert.pem -out key.pfx ``` ```csharp // // 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; ``` -------------------------------- ### Basic HTTP Authentication to Docker Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Details how to authenticate to a Docker instance using Basic HTTP Authentication. It covers installing the `Docker.DotNet.BasicAuth` package and using the `BasicAuthCredentials` type with username and password. ```bash PM> Install-Package Docker.DotNet.BasicAuth ``` ```csharp var credentials = new BasicAuthCredentials ("YOUR_USERNAME", "YOUR_PASSWORD"); var config = new DockerClientConfiguration("tcp://ubuntu-docker.cloudapp.net:4243", credentials); DockerClient client = config.CreateClient(); ``` -------------------------------- ### RestartPolicy C# Model with Enum Source: https://github.com/dotnet/docker.dotnet/blob/master/tools/specgen/README.md Example of a generated C# model for RestartPolicy, showcasing the use of an enum (RestartPolicyKind) for a property instead of its integer value. This customization is handled in specgen.go. ```csharp using System.Runtime.Serialization; namespace Docker.DotNet.Models { [DataContract] public class RestartPolicy // (container.RestartPolicy) { [DataMember(Name = "Name", EmitDefaultValue = false)] public RestartPolicyKind Name { get; set; } [DataMember(Name = "MaximumRetryCount", EmitDefaultValue = false)] public long MaximumRetryCount { get; set; } } } ``` -------------------------------- ### Create Docker Image from Registry Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Creates a Docker image by pulling it from a Docker Registry (e.g., Docker Hub). Authentication credentials can be provided, or anonymous download is possible. ```csharp await client.Images.CreateImageAsync( new ImagesCreateParameters { FromImage = "fedora/memcached", Tag = "alpha", }, new AuthConfig { Email = "test@example.com", Username = "test", Password = "pa$$w0rd" }, new Progress()); ``` -------------------------------- ### Update Docker Engine API Source Source: https://github.com/dotnet/docker.dotnet/blob/master/tools/specgen/README.md Instructions on how to update the Docker client engine-api source code using Go. It specifies the command to fetch the latest release tag and notes the versioning format for non-module Go libraries. ```bash > go get -u github.com/docker/docker@ ``` -------------------------------- ### Initialize Docker Client (Local Default) Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Initializes a Docker client to connect to the default local Docker daemon. This works for Docker for Windows using named pipes or Docker for Mac using Unix sockets. ```csharp using Docker.DotNet; DockerClient client = new DockerClientConfiguration() .CreateClient(); ``` -------------------------------- ### Generate Docker.DotNet Models Source: https://github.com/dotnet/docker.dotnet/blob/master/tools/specgen/README.md Command to regenerate C# models for Docker.DotNet after updating the engine-api source. This script should be run from the project's root directory. ```cmd > update-generated-code.cmd ``` -------------------------------- ### Initialize Docker Client (Custom Endpoint - Windows) Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Initializes a Docker client to connect to the default Docker Engine on Windows using a named pipe endpoint. ```csharp // Default Docker Engine on Windows using Docker.DotNet; DockerClient client = new DockerClientConfiguration( new Uri("npipe://./pipe/docker_engine")) .CreateClient(); ``` -------------------------------- ### Initialize Docker Client (Custom Endpoint - Linux) Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Initializes a Docker client to connect to the default Docker Engine on Linux using a Unix socket endpoint. ```csharp // Default Docker Engine on Linux using Docker.DotNet; DockerClient client = new DockerClientConfiguration( new Uri("unix:///var/run/docker.sock")) .CreateClient(); ``` -------------------------------- ### Monitor Docker Events Stream Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Demonstrates how to monitor Docker events, which are streamed continuously. It shows how to initiate the monitoring and how to cancel it using a CancellationToken. The stream format is JSON. ```json {"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} ... ``` ```csharp CancellationTokenSource cancellation = new CancellationTokenSource(); Stream stream = await client.System.MonitorEventsAsync(new ContainerEventsParameters(), new Progress(), cancellation.Token); // Initialize a StreamReader... ``` -------------------------------- ### Specify Docker Remote API Version Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Shows how to specify a particular version of the Docker Remote API when creating a Docker client. This is useful for leveraging version-specific features. ```csharp var config = new DockerClientConfiguration(...); DockerClient client = config.CreateClient(new Version(1, 16)); ``` -------------------------------- ### Initialize Docker Client (Remote Endpoint) Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Initializes a Docker client to connect to a remote Docker daemon endpoint. Requires specifying the remote endpoint URI. ```csharp using Docker.DotNet; DockerClient client = new DockerClientConfiguration( new Uri("http://ubuntu-docker.cloudapp.net:4243")) .CreateClient(); ``` -------------------------------- ### Create Docker Container Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Creates a new Docker container based on a specified image. Configuration options, such as DNS servers, can be provided via HostConfig. ```csharp await client.Containers.CreateContainerAsync(new CreateContainerParameters(){ Image = "fedora/memcached", HostConfig = new HostConfig() { DNS = new[] { "8.8.8.8", "8.8.4.4" } } }); ``` -------------------------------- ### List Docker Containers Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Retrieves a list of Docker containers, with an optional limit on the number of containers returned. This is an asynchronous operation. ```csharp IList containers = await client.Containers.ListContainersAsync( new ContainersListParameters(){ Limit = 10, }); ``` -------------------------------- ### Stop Docker Container Source: https://github.com/dotnet/docker.dotnet/blob/master/README.md Stops a running Docker container. It allows specifying a grace period before forcefully killing the container and supports cancellation via CancellationToken. ```csharp var stopped = await client.Containers.StopContainerAsync( "39e3317fd258", new ContainerStopParameters { WaitBeforeKillSeconds = 30 }, CancellationToken.None); ```