예제 #1
0
        private DockerContainer CreateContainer(string name, string image, string id)
        {
            var container = new DockerContainer
            {
                Name      = name,
                Id        = id,
                ImageName = image
            };

            return(container);
        }
예제 #2
0
        private async Task <IList <ContainerListResponse> > CreateNewContainer(string imageLabel, string imageTag)
        {
            var createContainerParameters = new CreateContainerParameters();

            createContainerParameters.Image = $"{imageLabel}:{imageTag}";
            createContainerParameters.Name  = $"{imageLabel}";

            var pbList = new List <PortBinding>
            {
                new PortBinding {
                    HostPort = "80"
                }
            };

            var portBindings = new Dictionary <string, IList <PortBinding> >
            {
                { "80/tcp", pbList }
            };

            createContainerParameters.HostConfig = new HostConfig
            {
                PortBindings    = portBindings,
                PublishAllPorts = true
            };

            _logger.LogInformation($"New container with image: {createContainerParameters.Image} is being created");
            var createContainerResponse = await _client.Containers.CreateContainerAsync(createContainerParameters);

            _logger.LogInformation($"New container with image: {createContainerParameters.Image} is being started");
            await _client.Containers.StartContainerAsync(createContainerResponse.ID, null);

            runningContainer = CreateContainer($"{imageLabel}", $"{imageLabel}:{imageTag}", createContainerResponse.ID);

            var containers = await GetAllContainers();

            foreach (var container in containers)
            {
                _logger.LogInformation($"New container with image: {container.Image} is running");
            }

            return(containers);
        }
예제 #3
0
        private async Task <IList <ContainerListResponse> > CheckForRunningContainers()
        {
            var containerFound = false;

            var containers = await GetAllContainers();

            foreach (var container in containers)
            {
                if (container.Names[0].Replace("/", "") == _defaultContainerName)
                {
                    runningContainer = CreateContainer(container.Names[0], container.Image, container.ID);
                    containerFound   = true;
                }
            }

            if (containerFound)
            {
                _logger.LogInformation($"Already running container found with Label:Tag = {runningContainer.ImageName}");
            }

            return(containers);
        }