Best Testcontainers-dotnet code snippet using DotNet.Testcontainers.Configurations.TestcontainersSettings.DockerImage
DockerRegistryAuthenticationProviderTest.cs
Source:DockerRegistryAuthenticationProviderTest.cs
...32 [InlineData("baz/.foo/bar:1.0.0", null)]33 [InlineData("baz/:foo/bar:1.0.0", null)]34 [InlineData("myregistry.azurecr.io/baz.foo/bar:1.0.0", "myregistry.azurecr.io")]35 [InlineData("myregistry.azurecr.io/baz:foo/bar:1.0.0", "myregistry.azurecr.io")]36 public void GetHostnameFromDockerImage(string dockerImageName, string hostname)37 {38 IDockerImage image = new DockerImage(dockerImageName);39 Assert.Equal(hostname, image.GetHostname());40 }41 [Theory]42 [InlineData("", "docker", "stable")]43 [InlineData("fedora", "httpd", "1.0")]44 [InlineData("foo/bar", "baz", "1.0.0")]45 public void GetHostnameFromHubImageNamePrefix(string repository, string name, string tag)46 {47 const string hubImageNamePrefix = "myregistry.azurecr.io";48 IDockerImage image = new DockerImage(repository, name, tag, hubImageNamePrefix);49 Assert.Equal(hubImageNamePrefix, image.GetHostname());50 }51 [Fact]52 public void ShouldGetDefaultDockerRegistryAuthenticationConfiguration()53 {54 var authenticationProvider = new DockerRegistryAuthenticationProvider("/tmp/docker.config", TestcontainersSettings.Logger);55 Assert.Equal(default(DockerRegistryAuthenticationConfiguration), authenticationProvider.GetAuthConfig(DockerRegistry));56 }57 public sealed class Base64ProviderTest58 {59 [Theory]60 [InlineData("{}", false)]61 [InlineData("{\"auths\":null}", false)]62 [InlineData("{\"auths\":{}}", false)]...
ImageFromDockerfileBuilder.cs
Source:ImageFromDockerfileBuilder.cs
...17 /// </summary>18 public ImageFromDockerfileBuilder()19 : this(new ImageFromDockerfileConfiguration(20 dockerEndpointAuthenticationConfiguration: TestcontainersSettings.OS.DockerEndpointAuthConfig,21 image: new DockerImage($"testcontainers:{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"),22 dockerfile: "Dockerfile",23 dockerfileDirectory: ".",24 labels: DefaultLabels.Instance,25 buildArguments: new Dictionary<string, string>()))26 {27 }28 /// <summary>29 /// Initializes a new instance of the <see cref="ImageFromDockerfileBuilder" /> class.30 /// </summary>31 /// <param name="dockerResourceConfiguration">The Docker image configuration.</param>32 private ImageFromDockerfileBuilder(IImageFromDockerfileConfiguration dockerResourceConfiguration)33 : base(dockerResourceConfiguration)34 {35 }36 /// <inheritdoc />37 public IImageFromDockerfileBuilder WithName(string name)38 {39 return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(image: new DockerImage(name)));40 }41 /// <inheritdoc />42 public IImageFromDockerfileBuilder WithName(IDockerImage name)43 {44 return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(image: name));45 }46 /// <inheritdoc />47 public IImageFromDockerfileBuilder WithDockerfile(string dockerfile)48 {49 return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(dockerfile: dockerfile));50 }51 /// <inheritdoc />52 public IImageFromDockerfileBuilder WithDockerfileDirectory(string dockerfileDirectory)53 {54 return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(dockerfileDirectory: dockerfileDirectory));55 }56 /// <inheritdoc />...
DockerImageNameSubstitutionTest.cs
Source:DockerImageNameSubstitutionTest.cs
...6 using DotNet.Testcontainers.Configurations;7 using DotNet.Testcontainers.Containers;8 using DotNet.Testcontainers.Images;9 using Xunit;10 [CollectionDefinition(nameof(DockerImageNameSubstitutionTest), DisableParallelization = true)]11 public static class DockerImageNameSubstitutionTest12 {13 [Collection(nameof(DockerImageNameSubstitutionTest))]14 public sealed class HubImageNamePrefixIsSet : IDisposable15 {16 public static IEnumerable<object[]> Substitutions { get; }17 = new[]18 {19 new[] { "my.proxy.com", "bar", "my.proxy.com/bar:latest" },20 new[] { "my.proxy.com", "bar:latest", "my.proxy.com/bar:latest" },21 new[] { "my.proxy.com", "bar:1.0.0", "my.proxy.com/bar:1.0.0" },22 new[] { "my.proxy.com/my-path", "bar:1.0.0", "my.proxy.com/my-path/bar:1.0.0" },23 new[] { "my.proxy.com:443", "bar:1.0.0", "my.proxy.com:443/bar:1.0.0" },24 new[] { "my.proxy.com", "foo/bar:1.0.0", "my.proxy.com/foo/bar:1.0.0" },25 new[] { "my.proxy.com/my-path", "foo/bar:1.0.0", "my.proxy.com/my-path/foo/bar:1.0.0" },26 new[] { "my.proxy.com:443", "foo/bar:1.0.0", "my.proxy.com:443/foo/bar:1.0.0" },27 new[] { "my.proxy.com:443/my-path", "foo/bar:1.0.0", "my.proxy.com:443/my-path/foo/bar:1.0.0" },28 new[] { "my.proxy.com", "myregistry.azurecr.io/foo/bar:1.0.0", "myregistry.azurecr.io/foo/bar:1.0.0" },29 new[] { "my.proxy.com", "myregistry.azurecr.io:443/foo/bar:1.0.0", "myregistry.azurecr.io:443/foo/bar:1.0.0" },30 };31 [Theory]32 [MemberData(nameof(Substitutions))]33 public void PrependForStringConfiguration(string hubImageNamePrefix, string imageName, string expectedFullName)34 {35 // Given36 TestcontainersSettings.HubImageNamePrefix = hubImageNamePrefix;37 // When38 IDockerContainer container = new TestcontainersBuilder<TestcontainersContainer>()39 .WithImage(imageName)40 .Build();41 // Then42 Assert.Equal(expectedFullName, container.Image.FullName);43 }44 [Theory]45 [MemberData(nameof(Substitutions))]46 public void PrependForObjectConfiguration(string hubImageNamePrefix, string imageName, string expectedFullName)47 {48 // Given49 TestcontainersSettings.HubImageNamePrefix = hubImageNamePrefix;50 IDockerImage image = new DockerImage(imageName);51 // When52 IDockerContainer container = new TestcontainersBuilder<TestcontainersContainer>()53 .WithImage(image)54 .Build();55 // Then56 Assert.Equal(expectedFullName, container.Image.FullName);57 }58 public void Dispose()59 {60 TestcontainersSettings.HubImageNamePrefix = string.Empty;61 }62 }63 [Collection(nameof(DockerImageNameSubstitutionTest))]64 public sealed class HubImageNamePrefixIsNotSet65 {66 public HubImageNamePrefixIsNotSet()67 {68 TestcontainersSettings.HubImageNamePrefix = string.Empty;69 }70 [Fact]71 public void DoNotPrependForStringConfiguration()72 {73 // Given74 const string imageName = "bar:latest";75 // When76 IDockerContainer container = new TestcontainersBuilder<TestcontainersContainer>()77 .WithImage(imageName)78 .Build();79 // Then80 Assert.Equal(imageName, container.Image.FullName);81 }82 [Fact]83 public void DoNotPrependForObjectConfiguration()84 {85 // Given86 const string imageName = "bar:latest";87 IDockerImage image = new DockerImage(imageName);88 // When89 IDockerContainer container = new TestcontainersBuilder<TestcontainersContainer>()90 .WithImage(image)91 .Build();92 // Then93 Assert.Equal(imageName, container.Image.FullName);94 }95 }96 }97}...
DockerImage
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers.Builders;4using DotNet.Testcontainers.Containers.Configurations;5using DotNet.Testcontainers.Containers.Modules;6using DotNet.Testcontainers.Containers.WaitStrategies;7using DotNet.Testcontainers.Images;8{9 {10 static async Task Main(string[] args)11 {12 var dockerImage = await new TestcontainersImage("alpine:3.12")13 .WithCommand("tail", "-f", "/dev/null")14 .BuildAsync();15 var container = new TestcontainersBuilder<Testcontainer>()16 .WithImage(dockerImage)17 .WithWaitStrategy(Wait.ForUnixContainer().UntilCommandIsCompleted("tail"))18 .Build();19 await container.StartAsync();20 Console.WriteLine($"Container ID: {container.Id}");21 Console.WriteLine($"Container Name: {container.Name}");22 Console.WriteLine($"Container IP Address: {container.Hostname}");23 Console.WriteLine($"Container Port: {container.GetMappedPublicPort(80)}");24 Console.WriteLine($"Container State: {container.State}");25 Console.WriteLine($"Container Image: {container.Image}");26 Console.WriteLine($"Container Created: {container.Created}");27 Console.WriteLine($"Container Started: {container.Started}");28 Console.WriteLine($"Container Finished: {container.Finished}");29 Console.WriteLine($"Container Status: {container.Status}");30 Console.WriteLine($"Container Logs: {container.Logs}");31 await container.StopAsync();32 }33 }34}
DockerImage
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers.Builders;4using DotNet.Testcontainers.Containers.Configurations;5using DotNet.Testcontainers.Containers.Modules;6using DotNet.Testcontainers.Containers.WaitStrategies;7{8 {9 static async Task Main(string[] args)10 {11 {12 WaitStrategies = Wait.ForUnixContainer().UntilCommandIsCompleted("echo Hello World")13 };14 var container = new TestcontainersBuilder<TestcontainersContainer>()15 .WithTestcontainersSettings(testcontainersSettings)16 .Build();17 await container.StartAsync();18 Console.WriteLine("Hello World!");19 }20 }21}22using System;23using System.Threading.Tasks;24using DotNet.Testcontainers.Containers.Builders;25using DotNet.Testcontainers.Containers.Configurations;26using DotNet.Testcontainers.Containers.Modules;27using DotNet.Testcontainers.Containers.WaitStrategies;28{29 {30 static async Task Main(string[] args)31 {32 {33 WaitStrategy = Wait.ForUnixContainer().UntilCommandIsCompleted("echo Hello World")34 };35 var container = new TestcontainersBuilder<TestcontainersContainer>()36 .WithContainerConfiguration(containerConfiguration)37 .Build();38 await container.StartAsync();39 Console.WriteLine("Hello World!");40 }41 }42}43using System;44using System.Threading.Tasks;45using DotNet.Testcontainers.Containers.Builders;46using DotNet.Testcontainers.Containers.Configurations;47using DotNet.Testcontainers.Containers.Modules;48using DotNet.Testcontainers.Containers.WaitStrategies;49{50 {
DockerImage
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers;4using DotNet.Testcontainers.Containers.Builders;5using DotNet.Testcontainers.Containers.Modules;6using DotNet.Testcontainers.Containers.WaitStrategies;7using DotNet.Testcontainers.Containers.Configurations;8using DotNet.Testcontainers.Containers.Configurations.Databases;9using DotNet.Testcontainers.Containers.Configurations.Abstractions;10using DotNet.Testcontainers.Containers.Configurations.Databases.Abstractions;11using DotNet.Testcontainers.Containers.Configurations.Databases.PostgreSql;12using DotNet.Testcontainers.Containers.Configurations.Databases.SqlServer;13using DotNet.Testcontainers.Containers.Configurations.Databases.MySql;14using DotNet.Testcontainers.Containers.Configurations.Databases.MongoDb;15using DotNet.Testcontainers.Containers.Configurations.Databases.Redis;16using DotNet.Testcontainers.Containers.Configurations.Databases.CouchDb;17using DotNet.Testcontainers.Containers.Configurations.Databases.RabbitMq;18using DotNet.Testcontainers.Containers.Configurations.Databases.Elasticsearch;19using DotNet.Testcontainers.Containers.Configurations.Databases.Cassandra;20using DotNet.Testcontainers.Containers.Configurations.Databases.Couchbase;21using DotNet.Testcontainers.Containers.Configurations.Databases.InfluxDb;22using DotNet.Testcontainers.Containers.Configurations.Databases.Kafka;23using DotNet.Testcontainers.Containers.Configurations.Databases.Memcached;24using DotNet.Testcontainers.Containers.Configurations.Databases.MssqlServer;25using DotNet.Testcontainers.Containers.Configurations.Databases.Neo4j;26using DotNet.Testcontainers.Containers.Configurations.Databases.OrientDb;27using DotNet.Testcontainers.Containers.Configurations.Databases.PostgreSql;28using DotNet.Testcontainers.Containers.Configurations.Databases.RabbitMq;29using DotNet.Testcontainers.Containers.Configurations.Databases.RethinkDb;30using DotNet.Testcontainers.Containers.Configurations.Databases.Solr;31using DotNet.Testcontainers.Containers.Configurations.Databases.Vault;32using DotNet.Testcontainers.Containers.Configurations.Databases.Zookeeper;33using DotNet.Testcontainers.Containers.Configurations.MessageBrokers;34using DotNet.Testcontainers.Containers.Configurations.MessageBrokers.Abstractions;35using DotNet.Testcontainers.Containers.Configurations.MessageBrokers.Kafka;36using DotNet.Testcontainers.Containers.Configurations.MessageBrokers.RabbitMq;
DockerImage
Using AI Code Generation
1var settings = new TestcontainersSettings();2settings.DockerImage("mcr.microsoft.com/dotnet/sdk:5.0");3var container = new TestcontainersBuilder<GenericContainer>()4.WithImage(settings)5.WithPortBinding(5000)6.Build();7await container.StartAsync();8Console.WriteLine($"Container Id: {container.Id}");9Console.WriteLine($"Container Name: {container.Name}");10Console.WriteLine($"Container Hostname: {container.Hostname}");11Console.WriteLine($"Container IPAddress: {container.GetContainerIpAddress()}");12Console.WriteLine($"Container Port: {container.GetMappedPublicPort(5000)}");13Console.WriteLine($"Container State: {container.State}");14await container.StopAsync();15await container.RemoveAsync();16var settings = new TestcontainersSettings();17settings.DockerImageName("mcr.microsoft.com/dotnet/sdk:5.0");18var container = new TestcontainersBuilder<GenericContainer>()19.WithImage(settings)20.WithPortBinding(5000)21.Build();22await container.StartAsync();23Console.WriteLine($"Container Id: {container.Id}");24Console.WriteLine($"Container Name: {container.Name}");25Console.WriteLine($"Container Hostname: {container.Hostname}");26Console.WriteLine($"Container IPAddress: {container.GetContainerIpAddress()}");27Console.WriteLine($"Container Port: {container.GetMappedPublicPort(5000)}");28Console.WriteLine($"Container State: {container.State}");29await container.StopAsync();30await container.RemoveAsync();31var settings = new TestcontainersSettings();32settings.DockerImage("mcr.microsoft.com/dotnet/sdk:5.0");33var container = new TestcontainersBuilder<GenericContainer>()34.WithImage(settings)35.WithPortBinding(5000)36.Build();37await container.StartAsync();38Console.WriteLine($"Container Id: {container.Id}");39Console.WriteLine($"Container Name: {container.Name}");40Console.WriteLine($"Container Hostname: {container.Hostname}");41Console.WriteLine($"Container IPAddress: {container.GetContainerIpAddress()}");42Console.WriteLine($"Container Port: {container.GetMappedPublicPort(5000)}");43Console.WriteLine($"Container State: {container.State}");44await container.StopAsync();45await container.RemoveAsync();
DockerImage
Using AI Code Generation
1{2};3{4};5{6};7{8};9{10};11{12};13{14};15{
DockerImage
Using AI Code Generation
1var container = new TestcontainersContainerBuilder<DotNetTestcontainersContainer>()2 .WithDockerEndpoint(dockerEndpoint)3 .WithImage("mcr.microsoft.com/dotnet/core/sdk:3.1")4 .WithExposedPort(8000)5 .Build();6var container = new TestcontainersContainerBuilder<DotNetTestcontainersContainer>()7 .WithDockerEndpoint(dockerEndpoint)8 .WithImage("mcr.microsoft.com/dotnet/core/sdk:3.1")9 .WithExposedPort(8000)10 .Build();11var container = new TestcontainersContainerBuilder<DotNetTestcontainersContainer>()12 .WithDockerEndpoint(dockerEndpoint)13 .WithImage("mcr.microsoft.com/dotnet/core/sdk:3.1")14 .WithExposedPort(8000)15 .Build();16var container = new TestcontainersContainerBuilder<DotNetTestcontainersContainer>()17 .WithDockerEndpoint(dockerEndpoint)18 .WithImage("mcr.microsoft.com/dotnet/core/sdk:3.1")19 .WithExposedPort(8000)20 .Build();21var container = new TestcontainersContainerBuilder<DotNetTestcontainersContainer>()22 .WithDockerEndpoint(dockerEndpoint)23 .WithImage("mcr.microsoft.com/dotnet/core/sdk:3.1")24 .WithExposedPort(8000)25 .Build();26var container = new TestcontainersContainerBuilder<DotNetTestcontainersContainer>()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!