public async Task RunMultipleContainers()
        {
            var dockerUrl = new Uri(uriString: GetEnvironmentVariable("DOCKER_URL"));
            var client = new DockerClientConfiguration(dockerUrl)
                 .CreateClient();
            var factory = new ContainerFactory(client);
            var container = factory.New(ContainerType.MongoDb);
            var container2 = factory.New(ContainerType.MongoDb);
            try
            {
                await container.CreateAsync();
                var started = await container.StartAsync();
                Assert.IsTrue(started);
                await Task.Delay(TimeSpan.FromSeconds(5));

                await container2.CreateAsync();
                started = await container2.StartAsync();
                Assert.IsTrue(started);
                await Task.Delay(TimeSpan.FromSeconds(5));
            }
            finally
            {
                await container.Stop();
                await container2.Stop();
                await container.Remove();
                await container2.Remove();
            }
        }
        public async Task GetPorts()
        {
            var dockerUrl = new Uri(uriString: GetEnvironmentVariable("DOCKER_URL"));
            var client = new DockerClientConfiguration(dockerUrl)
                 .CreateClient();
            var factory = new ContainerFactory(client);
            var container = factory.New(ContainerType.MongoDb);

            try
            {
                await container.CreateAsync();
                var started = await container.StartAsync();
                Assert.IsTrue(started);
                await Task.Delay(TimeSpan.FromSeconds(20));
                var ports = await container.GetPorts();
                Assert.AreEqual(28017, ports[0].PrivatePort);
                Assert.AreEqual(27017, ports[1].PrivatePort);

                Assert.IsTrue(ports[0].PublicPort > 0);
                Assert.IsTrue(ports[1].PublicPort > 0);
            }
            finally
            {
                await container.Stop();
                await container.Remove();
            }
        }
Ejemplo n.º 3
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            this.Configuration       = configuration;
            this.RequestedApiVersion = requestedApiVersion;
            this.JsonSerializer      = new JsonSerializer();

            this.Images        = new ImageOperations(this);
            this.Containers    = new ContainerOperations(this);
            this.Miscellaneous = new MiscellaneousOperations(this);
        }
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            this.Configuration = configuration;
            this.RequestedApiVersion = requestedApiVersion;
            this.JsonSerializer = new JsonSerializer();

            this.Images = new ImageOperations(this);
            this.Containers = new ContainerOperations(this);
            this.Miscellaneous = new MiscellaneousOperations(this);
        }
Ejemplo n.º 5
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration       = configuration;
            RequestedApiVersion = requestedApiVersion;
            JsonSerializer      = new JsonSerializer();

            Images        = new ImageOperations(this);
            Containers    = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);

            _client = new HttpClient(Configuration.Credentials.Handler, false);

            _defaultTimeout = _client.Timeout;

            _client.Timeout = InfiniteTimeout;
        }
Ejemplo n.º 6
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration = configuration;
            RequestedApiVersion = requestedApiVersion;
            JsonSerializer = new JsonSerializer();

            Images = new ImageOperations(this);
            Containers = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);

            _client = new HttpClient(Configuration.Credentials.Handler, false);

            _defaultTimeout = _client.Timeout;

            _client.Timeout = InfiniteTimeout;
        }
Ejemplo n.º 7
0
        public void Main(string[] args)
        {
            AnonymousCredentials creds = new AnonymousCredentials();

            DockerClient client = new DockerClientConfiguration(new Uri("http://127.0.0.1:4242")).CreateClient();

            var containers = client.Containers.ListContainersAsync(new Docker.DotNet.Models.ListContainersParameters() { All = true }).Result;

            foreach (var item in containers)
            {
                Console.WriteLine("Id: {0}", item.Id);
                Console.WriteLine("Names: {0}", string.Join(", ", item.Names));
                Console.WriteLine("Command: {0}", item.Command);
                Console.WriteLine("Created: {0}", item.Created);
                Console.WriteLine("Image: {0}", item.Image);
                Console.WriteLine("SizeRootFs: {0}", item.SizeRootFs);
                Console.WriteLine("SizeRw: {0}", item.SizeRw);
                Console.WriteLine("Status: {0}", item.Status);
            }
        }
Ejemplo n.º 8
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration        = configuration;
            _requestedApiVersion = requestedApiVersion;
            JsonSerializer       = new JsonSerializer();

            Images        = new ImageOperations(this);
            Containers    = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);
            Networks      = new NetworkOperations(this);

            ManagedHandler handler;
            var            uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                var pipeName   = uri.Segments[2];

                uri     = new UriBuilder("http", pipeName).Uri;
                handler = new ManagedHandler(async(host, port, cancellationToken) =>
                {
                    // NamedPipeClientStream handles file not found by polling until the server arrives. Use a short
                    // timeout so that the user doesn't get stuck waiting for a dockerd instance that is not running.
                    var timeout      = 100; // 100ms
                    var stream       = new NamedPipeClientStream(serverName, pipeName);
                    var dockerStream = new DockerPipeStream(stream);

#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                });

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri)
                {
                    Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
                };
                uri     = builder.Uri;
                handler = new ManagedHandler();
                break;

            case "https":
                handler = new ManagedHandler();
                break;

#if NETSTANDARD1_6
            case "unix":
                var pipeString = uri.LocalPath;
                handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
                {
                    var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                    await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
                    return(sock);
                });
                uri = new UriBuilder("http", uri.Segments.Last()).Uri;
                break;
#endif

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            _defaultTimeout = _client.Timeout;
            _client.Timeout = s_InfiniteTimeout;
        }
Ejemplo n.º 9
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration        = configuration;
            _requestedApiVersion = requestedApiVersion;
            JsonSerializer       = new JsonSerializer();

            Images     = new ImageOperations(this);
            Containers = new ContainerOperations(this);
            System     = new SystemOperations(this);
            Networks   = new NetworkOperations(this);
            Secrets    = new SecretsOperations(this);
            Swarm      = new SwarmOperations(this);
            Tasks      = new TasksOperations(this);
            Volumes    = new VolumeOperations(this);
            Plugin     = new PluginOperations(this);
            Exec       = new ExecOperations(this);

            ManagedHandler handler;
            var            uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                if (string.Equals(serverName, "localhost", StringComparison.OrdinalIgnoreCase))
                {
                    // npipe schemes dont work with npipe://localhost/... and need npipe://./... so fix that for a client here.
                    serverName = ".";
                }

                var pipeName = uri.Segments[2];

                uri     = new UriBuilder("http", pipeName).Uri;
                handler = new ManagedHandler(async(host, port, cancellationToken) =>
                {
                    int timeout      = (int)this.Configuration.NamedPipeConnectTimeout.TotalMilliseconds;
                    var stream       = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
                    var dockerStream = new DockerPipeStream(stream);

#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                });

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri)
                {
                    Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
                };
                uri     = builder.Uri;
                handler = new ManagedHandler();
                break;

            case "https":
                handler = new ManagedHandler();
                break;

#if (NETSTANDARD1_6 || NETSTANDARD2_0)
            case "unix":
                var pipeString = uri.LocalPath;
                handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
                {
                    var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                    await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
                    return(sock);
                });
                uri = new UriBuilder("http", uri.Segments.Last()).Uri;
                break;
#endif

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            DefaultTimeout  = Configuration.DefaultTimeout;
            _client.Timeout = s_InfiniteTimeout;
        }
Ejemplo n.º 10
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration       = configuration;
            RequestedApiVersion = requestedApiVersion;
            JsonSerializer      = new JsonSerializer();

            Images        = new ImageOperations(this);
            Containers    = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);
            Networks      = new NetworkOperations(this);

            ManagedHandler.StreamOpener opener;
            var uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                var pipeName   = uri.Segments[2];

                uri    = new UriBuilder("http", pipeName).Uri;
                opener = async(string host, int port, CancellationToken cancellationToken) =>
                {
                    // NamedPipeClientStream handles file not found by polling until the server arrives. Use a short
                    // timeout so that the user doesn't get stuck waiting for a dockerd instance that is not running.
                    int timeout      = 100; // 100ms
                    var stream       = new NamedPipeClientStream(serverName, pipeName);
                    var dockerStream = new DockerPipeStream(stream);
#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                };

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri);
                builder.Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http";
                uri            = builder.Uri;
                opener         = null;
                break;

            case "https":
                opener = null;
                break;

            //case "unix":
            // TODO

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            ManagedHandler handler;
            if (opener == null)
            {
                handler = new ManagedHandler();
            }
            else
            {
                handler = new ManagedHandler(opener);
            }

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            _defaultTimeout = _client.Timeout;
            _client.Timeout = InfiniteTimeout;
        }