public static CreateContainerParametersBuilder CreateFromContainer(Container container) { var imageId = new DockerImageId(container.ImageName, container.ImageVersion); var builder = new CreateContainerParametersBuilder(imageId, container.Name); foreach (var(name, value) in DockerLabelHelper.CreateForContainer(container)) { builder.AddLabel(name, value); } foreach (var binding in container.ServerPorts) { builder.AddPortBinding(binding.ContainerPort, binding.HostPort); } foreach (var volume in container.Volumes) { builder.AddVolumeMount( volume.DockerName, volume.ContainerMountPoint, DockerLabelHelper.CreateForVolume(container, volume) ); } foreach (var argument in container.Arguments) { builder.AddEnvironmentVariable(argument); } return(builder); }
private async Task <bool> DownloadImageAsync(DockerImageId imageId, DockerClient dockerClient) { using var loggerScope = _logger.BeginScope($"Download docker image {imageId.FullName}"); try { if (await CheckImageExistsAsync(imageId, dockerClient)) { return(true); } var parameters = new ImagesCreateParameters { FromImage = imageId.Name, Tag = imageId.Tag }; await dockerClient.Images.CreateImageAsync(parameters, null, new Progress <JSONMessage>()); _logger.LogInformation("Image downloaded"); return(true); } catch (Exception exception) { _logger.LogError(exception, "Failed to download image"); return(false); } }
public void TestThatMatchesMethodCorrectlyMatchesId() { var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { [@"/temp/some-directory"] = new MockDirectoryData() }); var source = new InternalDockerSource(fileSystem.DirectoryInfo.FromDirectoryName("/temp/some-directory"), new ImageConfig { Name = "Steve", Versions = new[] { new ImageVersion { Tag = "1.0" }, new ImageVersion { Tag = "2.0" } } }); var steveOneId = new DockerImageId("Steve", "1.0"); var steveTwoId = new DockerImageId("Steve", "2.0"); var steveThreeId = new DockerImageId("Steve", "3.0"); Assert.IsTrue(source.Matches(steveOneId)); Assert.IsTrue(source.Matches(steveTwoId)); Assert.IsFalse(source.Matches(steveThreeId)); }
private InternalDockerSource Find(DockerImageId imageId) { try { return(_internalSources.FirstOrDefault(s => s.Matches(imageId))); } catch { return(null); } }
private async Task <bool> PrepareImageAsync(DockerImageId imageId, DockerClient dockerClient) { using var loggerScope = _logger.BeginScope($"Preparing image {imageId.FullName}"); if (_imageSourceManager.IsInternalImage(imageId)) { var source = _imageSourceManager.Get(imageId); return(await BuildImageSourceAsync(imageId, source.Directory, dockerClient)); } return(await DownloadImageAsync(imageId, dockerClient)); }
private async Task <bool> BuildImageSourceAsync( DockerImageId imageId, IDirectoryInfo sourceDirectory, DockerClient dockerClient ) { using var loggerScope = _logger.BeginScope($"Building docker image from {sourceDirectory.FullName}"); var response = new LoggedResponse(_logger); try { if (await CheckImageExistsAsync(imageId, dockerClient)) { return(true); } _logger.LogInformation("Creating tar stream from directory"); var builder = new TarBuilder(); var sourceStream = await builder.BuildFromDirectory(sourceDirectory); _logger.LogInformation("Creating Docker image from tar stream"); using var dockerResponseStream = await dockerClient.Images.BuildImageFromDockerfileAsync(sourceStream, new ImageBuildParameters { Tags = new List <string> { imageId.FullName }, Labels = DockerLabelHelper.CreateBaseLabels() }); if (!await DockerBuildHelper.ValidateBuildAsync(dockerResponseStream, _logger)) { throw new Exception($"Docker build returned an error, lol soz (TODO: maybe log the error, Paul?)"); } return(true); } catch (Exception exception) { _logger.LogError(exception, "Unable to build Docker image"); } return(false); }
private async Task <bool> CheckImageExistsAsync(DockerImageId imageId, DockerClient dockerClient) { using var loggerScope = _logger.BeginScope($"Checking Docker image {imageId.FullName} exists locally"); try { await dockerClient.Images.InspectImageAsync(imageId.FullName); _logger.LogInformation("The image exists already"); return(true); } catch { _logger.LogInformation("The image does not exist"); return(false); } }
public static CreateContainerParametersBuilder CreateStorageServer(Server server) { var imageId = new DockerImageId("triceratops_volumeinspector", "1.0"); var containerName = NameHelper.SanitiseHostname($"Triceratops.StorageServer.{server.Name}"); var builder = new CreateContainerParametersBuilder(imageId, containerName) .AddLabels(DockerLabelHelper.CreateForTemporaryContainer()); foreach (var volume in ServerVolumeIdentifier.CreateForServer(server)) { builder.AddVolumeMount( volume.VolumeName, volume.CreateMountDestination(Constants.VolumeInspectorVolumesPath), new Dictionary <string, string>(0) ); } ; return(builder); }
public CreateContainerParametersBuilder(DockerImageId imageId, string containerName) { ImageId = imageId; ContainerName = containerName; NetworkName = DockerServiceConstants.TriceratopsNetwork; }
public bool IsInternalImage(DockerImageId imageId) { return(Find(imageId) != null); }
public InternalDockerSource Get(DockerImageId imageId) { return(Find(imageId)); }
public bool Matches(DockerImageId imageId) { return(Config.Name == imageId.Name && Config.Versions.Any(v => v.Tag == imageId.Tag)); }