How to use TestcontainersContainer class of DotNet.Testcontainers.Containers package

Best Testcontainers-dotnet code snippet using DotNet.Testcontainers.Containers.TestcontainersContainer

LocalStackFixture.cs

Source:LocalStackFixture.cs Github

copy

Full Screen

...11namespace Armut.Tests.Common.Fixtures12{13 public class LocalStackFixture : IAsyncLifetime14 {15 private readonly TestcontainersContainer _localStackContainer;16 public LocalStackFixture()17 {18 ITestcontainersBuilder<TestcontainersContainer> localStackBuilder =19 new TestcontainersBuilder<TestcontainersContainer>()20 .WithName($"LocalStack-0.12.6-{DateTime.Now.Ticks}")21 .WithImage("localstack/localstack:0.12.6")22 .WithMount("/var/run/docker.sock", "/var/run/docker.sock")23 .WithCleanUp(true)24 .WithEnvironment("DEFAULT_REGION", "eu-central-1")25 .WithEnvironment("DATA_DIR", "/tmp/localstack/data")26 .WithEnvironment("SERVICES", "iam,lambda,dynamodb,apigateway,s3,sns,cloudformation,cloudwatch,sts")27 .WithEnvironment("DOCKER_HOST", "unix:///var/run/docker.sock")28 .WithEnvironment("LAMBDA_EXECUTOR", "docker-reuse")29 .WithEnvironment("LS_LOG", "debug")30 .WithEnvironment("DOCKER_GATEWAY_HOST", "172.17.0.1")31 //.WithEnvironment("LAMBDA_DOCKER_DNS", "172.17.0.1")32 //.WithEnvironment("HOSTNAME", "172.17.0.1")33 //.WithEnvironment("HOSTNAME_EXTERNAL", "172.17.0.1")34 //.WithEnvironment("LOCALSTACK_HOSTNAME", "172.17.0.1")35 //.WithEnvironment("MAIN_CONTAINER_NAME", "LocalStack-0.12.6")36 .WithPortBinding(4566, 4566);37 _localStackContainer = localStackBuilder.Build();38 ReplaceMountValue(_localStackContainer, new List<IBind>() { new WslMount("/var/run/docker.sock", "/var/run/docker.sock", AccessMode.ReadWrite) });39 }40 public async Task InitializeAsync()41 {42 await _localStackContainer.StartAsync();43 }44 public async Task DisposeAsync()45 {46 await _localStackContainer.StopAsync();47 }48 private static void ReplaceMountValue(TestcontainersContainer testcontainersContainer, IEnumerable<IBind> wslMounts)49 {50 FieldInfo configurationFieldInfo = testcontainersContainer.GetType().GetField("configuration", BindingFlags.Instance | BindingFlags.NonPublic);51 var configuration = (ITestcontainersConfiguration) configurationFieldInfo.GetValue(testcontainersContainer);52 PropertyInfo propertyInfo = configuration53 .GetType()54 .GetProperty(nameof(configuration.Mounts), BindingFlags.Public | BindingFlags.Instance);55 FieldInfo backingField = GetBackingField(propertyInfo);56 backingField.SetValue(configuration, wslMounts);57 }58 private static FieldInfo GetBackingField(PropertyInfo pi) {59 if (!pi.CanRead || !pi.GetGetMethod(nonPublic:true).IsDefined(typeof(CompilerGeneratedAttribute), inherit:true))60 return null;61 var backingField = pi.DeclaringType.GetField($"<{pi.Name}>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);62 if (backingField == null)...

Full Screen

Full Screen

KafkaDockerContainer.cs

Source:KafkaDockerContainer.cs Github

copy

Full Screen

...37 {38 await _kafkaContainer.DisposeAsync();39 await _zookeeperContainer.DisposeAsync();40 }41 private TestcontainersContainer BuildKafkaContainer() =>42 new TestcontainersBuilder<TestcontainersContainer>()43 .WithImage(KafkaConfiguration.DockerImage)44 .WithName(KafkaConfiguration.ContainerName)45 .WithHostname(KafkaConfiguration.ContainerName)46 .WithEnvironmentVariablesFrom(KafkaConfiguration)47 .WithPortBindingsFrom(KafkaConfiguration)48 .WithExposedPortsFrom(KafkaConfiguration)49 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(KafkaConfiguration.BootstrapPort))50 .WithCleanUp(cleanUp: true)51 .Build();52 private TestcontainersContainer BuildZookeeperContainer() =>53 new TestcontainersBuilder<TestcontainersContainer>()54 .WithImage(ZookeeperConfiguration.DockerImage)55 .WithName(ZookeeperConfiguration.ContainerName)56 .WithHostname(ZookeeperConfiguration.ContainerName)57 .WithEnvironmentVariablesFrom(ZookeeperConfiguration)58 .WithPortBindingsFrom(ZookeeperConfiguration)59 .WithExposedPortsFrom(ZookeeperConfiguration)60 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(ZookeeperConfiguration.ExposedHttpPort))61 .WithCleanUp(cleanUp: true)62 .Build();63 private IDockerContainer _kafkaContainer;64 private IDockerContainer _zookeeperContainer;65 }66}...

Full Screen

Full Screen

ContainerService.cs

Source:ContainerService.cs Github

copy

Full Screen

...6public class ContainerService7{8 private readonly ILogger<ContainerService> _logger;9 private IDockerNetwork? _network;10 private TestcontainersContainer? _containers;11 private TestcontainersContainer? _containers2;12 public ContainerService(ILogger<ContainerService> logger)13 {14 _logger = logger;15 }16 public async Task RunContainers()17 {18 var networkBuilder = new TestcontainersNetworkBuilder()19 .WithName("elastic")20 .WithDriver(NetworkDriver.Bridge);21 _network = networkBuilder.Build();22 _logger.LogInformation("Creating Network");23 await _network.CreateAsync();24 var name = "elasticsearch-dotnet-samples";25 var containersBuilder = new TestcontainersBuilder<TestcontainersContainer>()26 .WithImage("elasticsearch:8.3.3")27 .WithName(name)28 .WithEnvironment("xpack.security.enabled", "false")29 .WithEnvironment("discovery.type", "single-node")30 .WithPortBinding(9200)31 .WithPortBinding(9300)32 .WithNetwork(_network)33 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(9200));34 var containersBuilder2 = new TestcontainersBuilder<TestcontainersContainer>()35 .WithImage("kibana:8.3.3")36 .WithName("kibana-dotnet-samples")37 .WithEnvironment("ELASTICSEARCH_HOSTS", $"http://{name}:9200")38 .WithPortBinding(5601)39 .WithNetwork(_network);40 _logger.LogInformation("Building Containers");41 _containers = containersBuilder.Build();42 _containers2 = containersBuilder2.Build();43 _logger.LogInformation("Starting Containers");44 await _containers.StartAsync();45 await _containers2.StartAsync();46 }47 public async Task StopContainers()48 {...

Full Screen

Full Screen

TestcontainersContainer

Using AI Code Generation

copy

Full Screen

1using DotNet.Testcontainers.Containers;2using DotNet.Testcontainers.Containers.Builders;3using DotNet.Testcontainers.Containers.Modules;4using DotNet.Testcontainers.Containers.Modules.Databases;5using DotNet.Testcontainers.Containers.WaitStrategies;6using System;7using System.Threading.Tasks;8{9 {10 static async Task Main(string[] args)11 {12 var builder = new TestcontainersBuilder<TestcontainersContainer>()13 .WithImage("mcr.microsoft.com/mssql/server:2019-latest")14 .WithEnvironment("ACCEPT_EULA", "Y")15 .WithEnvironment("SA_PASSWORD", "Password123")16 .WithPortBinding(1433, 1433)17 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433));18 var container = builder.Build();19 await container.StartAsync();20 Console.WriteLine("Hello World!");21 }22 }23}24using DotNet.Testcontainers.Containers;25using DotNet.Testcontainers.Containers.Builders;26using DotNet.Testcontainers.Containers.Modules;27using DotNet.Testcontainers.Containers.Modules.Databases;28using DotNet.Testcontainers.Containers.WaitStrategies;29using System;30using System.Threading.Tasks;31{32 {33 static async Task Main(string[] args)34 {35 var builder = new TestcontainersBuilder<TestcontainersContainer>()36 .WithImage("mcr.microsoft.com/mssql/server:2019-latest")37 .WithEnvironment("ACCEPT_EULA", "Y")38 .WithEnvironment("SA_PASSWORD", "Password123")39 .WithPortBinding(1433, 1433)40 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433));41 var container = builder.Build();42 await container.StartAsync();43 Console.WriteLine("Hello World!");44 }45 }46}47using DotNet.Testcontainers.Containers;48using DotNet.Testcontainers.Containers.Builders;49using DotNet.Testcontainers.Containers.Modules;50using DotNet.Testcontainers.Containers.Modules.Databases;51using DotNet.Testcontainers.Containers.WaitStrategies;52using System;53using System.Threading.Tasks;54{

Full Screen

Full Screen

TestcontainersContainer

Using AI Code Generation

copy

Full Screen

1using DotNet.Testcontainers.Containers;2using DotNet.Testcontainers.Containers.Modules;3using DotNet.Testcontainers.Containers.Modules.Databases;4using DotNet.Testcontainers.Containers.WaitStrategies;5using System;6using System.Threading.Tasks;7{8 {9 static async Task Main(string[] args)10 {11 var container = new TestcontainersContainer()12 .WithImage("postgres:13.1")13 .WithDatabase(new PostgreSqlTestcontainerConfiguration("postgres", "postgres"))14 .WithPortBinding(5432)15 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(5432))16 .WithEnvironment("POSTGRES_USER", "postgres")17 .WithEnvironment("POSTGRES_PASSWORD", "postgres")18 .WithEnvironment("POSTGRES_DB", "postgres");19 await container.StartAsync();20 Console.WriteLine("Hello World!");21 }22 }23}24using DotNet.Testcontainers.Containers;25using DotNet.Testcontainers.Containers.Modules;26using DotNet.Testcontainers.Containers.Modules.Databases;27using DotNet.Testcontainers.Containers.WaitStrategies;28using System;29using System.Threading.Tasks;30{31 {32 static async Task Main(string[] args)33 {34 var container = new TestcontainersBuilder<PostgreSqlTestcontainer>()35 .WithImage("postgres:13.1")36 .WithDatabase(new PostgreSqlTestcontainerConfiguration("postgres", "postgres"))37 .WithPortBinding(5432)38 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(5432))39 .WithEnvironment("POSTGRES_USER", "postgres")40 .WithEnvironment("POSTGRES_PASSWORD", "postgres")41 .WithEnvironment("POSTGRES_DB", "postgres")42 .Build();43 await container.StartAsync();44 Console.WriteLine("Hello World!");45 }46 }47}48using DotNet.Testcontainers.Containers;49using DotNet.Testcontainers.Containers.Modules;50using DotNet.Testcontainers.Containers.Modules.Databases;51using DotNet.Testcontainers.Containers.WaitStrategies;52using System;53using System.Threading.Tasks;54{55 {56 static async Task Main(string[]

Full Screen

Full Screen

TestcontainersContainer

Using AI Code Generation

copy

Full Screen

1using DotNet.Testcontainers.Containers;2using DotNet.Testcontainers.Containers.Builders;3using System;4using System.Threading.Tasks;5{6 {7 static async Task Main(string[] args)8 {9 using (var container = new TestcontainersContainerBuilder<TestcontainersContainer>()10 .WithImage("mcr.microsoft.com/mssql/server")11 .WithPortBinding(1433, 1433)12 .WithEnvironment("ACCEPT_EULA", "Y")13 .WithEnvironment("SA_PASSWORD", "P@ssw0rd")14 .Build())15 {16 await container.StartAsync();17 Console.WriteLine($"Container ID: {container.Id}");18 Console.WriteLine($"Container Name: {container.Name}");19 Console.WriteLine($"Container Hostname: {container.Hostname}");20 Console.WriteLine($"Container IP Address: {container.HostIpAddress}");21 Console.WriteLine($"Container Port: {container.MappedPublicPort}");22 Console.WriteLine($"Container State: {container.State}");23 Console.WriteLine($"Container Status: {container.Status}");24 Console.WriteLine($"Container Logs: {container.Logs}");25 Console.WriteLine($"Container Created: {container.Created}");26 Console.WriteLine($"Container Started: {container.Started}");27 Console.WriteLine($"Container Finished: {container.Finished}");28 Console.WriteLine($"Container Image: {container.Image}");29 Console.WriteLine($"Container Image ID: {container.ImageId}");30 Console.WriteLine($"Container Command: {container.Command}");31 Console.WriteLine($"Container Labels: {container.Labels}");32 Console.WriteLine($"Container Mounts: {container.Mounts}");33 Console.WriteLine($"Container Networks: {container.Networks}");34 Console.WriteLine($"Container Ports: {container.Ports}");35 Console.WriteLine($"Container Size Root FS: {container.SizeRootFs}");36 Console.WriteLine($"Container Size RW: {container.SizeRw}");37 Console.WriteLine($"Container Uptime: {container.Uptime}");38 Console.WriteLine($"Container Working Dir: {container.WorkingDir}");39 Console.WriteLine($"Container Exposed Ports: {container.ExposedPorts}");40 Console.WriteLine($"Container Host Config: {container.HostConfig}");41 Console.WriteLine($"Container Network Settings: {container.NetworkSettings}");42 Console.WriteLine($"Container Platform: {container.Platform}");43 Console.WriteLine($"Container Mount Labels: {container.MountLabels}");44 Console.WriteLine($"Container Healthcheck: {container.Healthcheck}");45 Console.WriteLine($"Container App Armor Profile: {container.AppArmorProfile}");

Full Screen

Full Screen

TestcontainersContainer

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers;4using DotNet.Testcontainers.Containers.Modules.Databases;5{6 {7 static async Task Main(string[] args)8 {9 using (var postgresContainer = new TestcontainersContainer()10 .WithDatabase(new PostgreSqlTestcontainerConfiguration("postgres:latest"))11 .WithExposedPort(5432)12 .WithEnvironment("POSTGRES_PASSWORD", "postgres")13 .WithEnvironment("POSTGRES_USER", "postgres")14 .WithEnvironment("POSTGRES_DB", "postgres")15 .WithCleanUp(true)16 .WithImage("postgres:latest")17 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(5432))18 .StartAsync())19 {20 Console.WriteLine($"Postgres container is ready. Connection string: {postgresContainer.ConnectionString}");21 Console.ReadLine();22 }23 }24 }25}26using System;27using System.Threading.Tasks;28using DotNet.Testcontainers.Containers;29using DotNet.Testcontainers.Containers.Modules.Databases;30using DotNet.Testcontainers.Containers.WaitStrategies;31using DotNet.Testcontainers.Images;32{33 {34 static async Task Main(string[] args)35 {36 var postgresContainer = new TestcontainersBuilder<PostgreSqlTestcontainer>()37 .WithImage(new Image("postgres:latest"))38 .WithDatabase(new PostgreSqlTestcontainerConfiguration("postgres:latest"))39 .WithExposedPort(5432)40 .WithEnvironment("POSTGRES_PASSWORD", "postgres")41 .WithEnvironment("POSTGRES_USER", "postgres")42 .WithEnvironment("POSTGRES_DB", "postgres")43 .WithCleanUp(true)44 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(5432))45 .Build();46 await postgresContainer.StartAsync();47 Console.WriteLine($"Postgres container is

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful