Пример #1
0
        /// <inheritdoc />
        public async Task <IEnumerable <ContainerListResponse> > RetrieveAllContainersAsync(int skip = 0, int take = 20)
        {
            using var connection = await _dockerClient.ConnectAsync();

            var containers = connection.Containers;

            try
            {
                var request = new ContainersListParameters
                {
                    All   = true,
                    Limit = skip + take,
                };
                var response = await containers.ListContainersAsync(request);

                _logger.LogInformation("Successfully retrieved '{Containers}' container(s).", response.Count);

                return(response.Skip(skip).Take(take));
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Could not retrieve any containers.");
                return(null);
            }
        }
Пример #2
0
        public async Task <bool> ConnectNetworkAsync(string id, string containerId)
        {
            using var connection = await _dockerClient.ConnectAsync();

            var networks = connection.Networks;

            try
            {
                var parameters = new NetworkConnectParameters
                {
                    Container = containerId
                };
                await networks.ConnectNetworkAsync(id, parameters);

                _logger.LogInformation("Successfully connected container '{ContainerId}' to network with id '{Id}'.",
                                       containerId, id);

                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Could not connect container '{ContainerId}' to network with id '{Id}'.",
                                   containerId, id);
                return(false);
            }
        }
Пример #3
0
        public async Task <bool> StartContainerAsync(string id)
        {
            using var connection = await _dockerClient.ConnectAsync();

            var containers = connection.Containers;

            try
            {
                var parameters = new ContainerStartParameters();
                var response   = await containers.StartContainerAsync(id, parameters);

                _logger.LogInformation("Container with '{Id}' started successfully.", id);

                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Container with id '{Id}' could not be stopped.", id);
                return(false);
            }
        }
Пример #4
0
        public async Task <ImageInspectResponse> CreateImageAsync(string image, string tag = "latest", string username = null, string password = null)
        {
            using var connection = await _dockerClient.ConnectAsync();

            var images = connection.Images;

            try
            {
                var imageParameters = new ImagesCreateParameters
                {
                    FromImage = image,
                    Tag       = tag ?? "latest"
                };
                var authConfig = new AuthConfig
                {
                    Username = username,
                    Password = password
                };

                var registry = image.Split('/').First();
                if (registry.Contains('.') && username != null && password != null)
                {
                    await _authService.AuthenticateAsync(registry, username, password);
                }

                await images.CreateImageAsync(imageParameters, authConfig, new Progress <JSONMessage>());

                _logger.LogInformation("Successfully finished pulling the image '{Image}'.", image);

                var inspectImage = await RetrieveImageAsync($"{image}:{imageParameters.Tag}");

                return(inspectImage);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Image '{Image}' could not be pulled successfully.", image);
                return(null);
            }
        }
Пример #5
0
        public async Task <IEnumerable <NetworkResponse> > RetrieveAllNetworksAsync(int skip = 0, int take = 20)
        {
            using var connection = await _dockerClient.ConnectAsync();

            var networks = connection.Networks;

            try
            {
                var response = await networks.ListNetworksAsync();

                var filtered = response.Skip(skip).Take(take)
                               .Select(async x => await RetrieveNetworkAsync(x.ID))
                               .Select(x => x.Result).ToList();
                _logger.LogInformation("Successfully retrieved '{Count}' networks.", filtered.Count);

                return(filtered);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Could not retrieve any networks.");
                return(null);
            }
        }