public void DockerfileArchiveTar()
        {
            // Given
            var image = new DockerImage("Testcontainers", "Test", "1.0.0");

            var expected = new SortedSet <string> {
                "Dockerfile", "setup/setup.sh"
            };

            var actual = new SortedSet <string>();

            var dockerFileArchive = new DockerfileArchive("Assets", "Dockerfile", image, TestcontainersSettings.Logger);

            // When
            using (var tarOut = new FileInfo(dockerFileArchive.Tar()).OpenRead())
            {
                using (var tarIn = TarArchive.CreateInputTarArchive(tarOut, Encoding.UTF8))
                {
                    tarIn.ProgressMessageEvent += (_, entry, _) => actual.Add(entry.Name);
                    tarIn.ListContents();
                }
            }

            // Then
            Assert.Equal(expected, actual);
        }
Exemplo n.º 2
0
        public async Task <string> BuildAsync(ImageFromDockerfileConfiguration config, CancellationToken cancellationToken = default)
        {
            var dockerFileArchive = new DockerfileArchive(config.DockerfileDirectory);

            await MetaDataClientImages.Instance.ExistsWithNameAsync(config.Image).ContinueWith(async imageExists =>
            {
                if (!await imageExists && config.DeleteIfExists)
                {
                    await Docker.Images.DeleteImageAsync(config.Image, new ImageDeleteParameters {
                        Force = true
                    }, cancellationToken);
                }
            });

            using (var stream = new FileStream(dockerFileArchive.Tar(), FileMode.Open))
            {
                using (var builtImage = await Docker.Images.BuildImageFromDockerfileAsync(stream, new ImageBuildParameters {
                    Dockerfile = "Dockerfile", Tags = new[] { config.Image }
                }, cancellationToken))
                {
                    // New Docker image built, ready to use.
                }
            }

            return(config.Image);
        }
        public async Task <string> BuildAsync(IImageFromDockerfileConfiguration config, CancellationToken ct = default)
        {
            var dockerFileArchive = new DockerfileArchive(config.DockerfileDirectory, config.Dockerfile, config.Image);

            var imageExists = await this.ExistsWithNameAsync(config.Image.FullName, ct)
                              .ConfigureAwait(false);

            if (imageExists && config.DeleteIfExists)
            {
                await this.DeleteAsync(config.Image, ct)
                .ConfigureAwait(false);
            }

            using (var stream = new FileStream(dockerFileArchive.Tar(), FileMode.Open))
            {
                using (var image = await this.Docker.Images.BuildImageFromDockerfileAsync(stream, new ImageBuildParameters {
                    Dockerfile = config.Dockerfile, Tags = new[] { config.Image.FullName }
                }, ct)
                                   .ConfigureAwait(false))
                {
                    // Read the image stream to the end, to avoid disposing before Docker has done it's job.
                    _ = await new StreamReader(image).ReadToEndAsync()
                        .ConfigureAwait(false);
                }
            }

            return(config.Image.FullName);
        }
Exemplo n.º 4
0
        public async Task <string> BuildAsync(IImageFromDockerfileConfiguration configuration, CancellationToken ct = default)
        {
            var image = configuration.Image;

            ITarArchive dockerFileArchive = new DockerfileArchive(configuration.DockerfileDirectory, configuration.Dockerfile, image, this.logger);

            var imageExists = await this.ExistsWithNameAsync(image.FullName, ct)
                              .ConfigureAwait(false);

            if (imageExists && configuration.DeleteIfExists)
            {
                await this.DeleteAsync(image, ct)
                .ConfigureAwait(false);
            }

            var buildParameters = new ImageBuildParameters
            {
                Dockerfile = configuration.Dockerfile,
                Tags       = new[] { image.FullName },
                Labels     = configuration.Labels.ToDictionary(item => item.Key, item => item.Value),
            };

            using (var dockerFileStream = new FileStream(dockerFileArchive.Tar(), FileMode.Open))
            {
                await this.Docker.Images.BuildImageFromDockerfileAsync(buildParameters, dockerFileStream, Array.Empty <AuthConfig>(), new Dictionary <string, string>(), this.traceProgress, ct)
                .ConfigureAwait(false);
            }

            this.logger.DockerImageBuilt(image);
            return(image.FullName);
        }
        public void DockerfileArchiveTar()
        {
            // Given
            var expected = new List <string> {
                "Dockerfile", "setup", "setup/setup.sh"
            };

            var actual = new List <string>();

            var dockerFileArchive = new DockerfileArchive("./Assets");

            using (var tarOut = new FileInfo(dockerFileArchive.Tar()).OpenRead())
            {
                using (var tarIn = TarArchive.CreateInputTarArchive(tarOut))
                {
                    tarIn.ProgressMessageEvent += (archive, entry, message) => actual.Add(entry.Name);
                    tarIn.ListContents();
                }
            }

            Assert.Equal(expected, actual);
        }
        public async Task <string> BuildAsync(IImageFromDockerfileConfiguration config, CancellationToken ct = default)
        {
            var dockerFileArchive = new DockerfileArchive(config.DockerfileDirectory);

            var imageExists = await this.ExistsWithNameAsync(config.Image.FullName, ct);

            if (imageExists && config.DeleteIfExists)
            {
                await this.DeleteAsync(config.Image, ct);
            }

            using (var stream = new FileStream(dockerFileArchive.Tar(), FileMode.Open))
            {
                using (var unused = await this.Docker.Images.BuildImageFromDockerfileAsync(stream, new ImageBuildParameters {
                    Dockerfile = config.Dockerfile, Tags = new[] { config.Image.FullName }
                }, ct))
                {
                    // New Docker image built, ready to use.
                }
            }

            return(config.Image.FullName);
        }