Best Testcontainers-dotnet code snippet using DotNet.Testcontainers.Builders.ImageFromDockerfileBuilder.ImageFromDockerfileBuilder
ImageFromDockerfileBuilder.cs
Source:ImageFromDockerfileBuilder.cs
...7 using DotNet.Testcontainers.Clients;8 using DotNet.Testcontainers.Configurations;9 using DotNet.Testcontainers.Images;10 using JetBrains.Annotations;11 /// <inheritdoc cref="IImageFromDockerfileBuilder" />12 [PublicAPI]13 public class ImageFromDockerfileBuilder : AbstractBuilder<IImageFromDockerfileBuilder, IImageFromDockerfileConfiguration>, IImageFromDockerfileBuilder14 {15 /// <summary>16 /// Initializes a new instance of the <see cref="ImageFromDockerfileBuilder" /> class.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 />57 public IImageFromDockerfileBuilder WithDockerfileDirectory(CommonDirectoryPath commonDirectoryPath, string dockerfileDirectory)58 {59 var baseDirectoryPath = Path.Combine(commonDirectoryPath.DirectoryPath, dockerfileDirectory);60 return this.WithDockerfileDirectory(baseDirectoryPath);61 }62 /// <inheritdoc />63 public IImageFromDockerfileBuilder WithDeleteIfExists(bool deleteIfExists)64 {65 return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(deleteIfExists: deleteIfExists));66 }67 /// <inheritdoc />68 public IImageFromDockerfileBuilder WithBuildArgument(string name, string value)69 {70 var buildArgs = new Dictionary<string, string> { { name, value } };71 return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(buildArguments: buildArgs));72 }73 /// <inheritdoc />74 public Task<string> Build()75 {76 _ = Guard.Argument(this.DockerResourceConfiguration.DockerEndpointAuthConfig, nameof(IDockerResourceConfiguration.DockerEndpointAuthConfig))77 .DockerEndpointAuthConfigIsSet();78 ITestcontainersClient client = new TestcontainersClient(this.DockerResourceConfiguration.SessionId, this.DockerResourceConfiguration.DockerEndpointAuthConfig, TestcontainersSettings.Logger);79 return client.BuildAsync(this.DockerResourceConfiguration);80 }81 /// <inheritdoc />82 protected override IImageFromDockerfileBuilder MergeNewConfiguration(IDockerResourceConfiguration dockerResourceConfiguration)83 {84 return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(dockerResourceConfiguration));85 }86 /// <summary>87 /// Merges the current with the new Docker resource configuration.88 /// </summary>89 /// <param name="dockerResourceConfiguration">The new Docker resource configuration.</param>90 /// <returns>A configured instance of <see cref="IImageFromDockerfileBuilder" />.</returns>91 protected virtual IImageFromDockerfileBuilder MergeNewConfiguration(IImageFromDockerfileConfiguration dockerResourceConfiguration)92 {93 var dockerEndpointAuthConfig = BuildConfiguration.Combine(dockerResourceConfiguration.DockerEndpointAuthConfig, this.DockerResourceConfiguration.DockerEndpointAuthConfig);94 var image = BuildConfiguration.Combine(dockerResourceConfiguration.Image, this.DockerResourceConfiguration.Image);95 var dockerfile = BuildConfiguration.Combine(dockerResourceConfiguration.Dockerfile, this.DockerResourceConfiguration.Dockerfile);96 var dockerfileDirectory = BuildConfiguration.Combine(dockerResourceConfiguration.DockerfileDirectory, this.DockerResourceConfiguration.DockerfileDirectory);97 var deleteIfExists = dockerResourceConfiguration.DeleteIfExists && this.DockerResourceConfiguration.DeleteIfExists;98 var labels = BuildConfiguration.Combine(dockerResourceConfiguration.Labels, this.DockerResourceConfiguration.Labels);99 var buildArgs = BuildConfiguration.Combine(dockerResourceConfiguration.BuildArguments, this.DockerResourceConfiguration.BuildArguments);100 return new ImageFromDockerfileBuilder(new ImageFromDockerfileConfiguration(dockerEndpointAuthConfig, image, dockerfile, dockerfileDirectory, deleteIfExists, labels, buildArgs));101 }102 }103}...
ImageFromDockerfileTest.cs
Source:ImageFromDockerfileTest.cs
...37 public async Task DockerfileDoesNotExist()38 {39 // Given40 var exception = await Assert.ThrowsAsync<ArgumentException>(() =>41 new ImageFromDockerfileBuilder()42 .WithDockerfile("Dockerfile")43 .WithDockerfileDirectory(".")44 .Build());45 // When46 // Then47 Assert.Equal($"Dockerfile does not exist in '{new DirectoryInfo(".").FullName}'.", exception.Message);48 }49 [Fact]50 public async Task DockerfileDirectoryDoesNotExist()51 {52 // Given53 var exception = await Assert.ThrowsAsync<ArgumentException>(() =>54 new ImageFromDockerfileBuilder()55 .WithDockerfileDirectory("DoesNotExist")56 .Build());57 // When58 // Then59 Assert.Equal($"Directory '{new DirectoryInfo("DoesNotExist").FullName}' does not exist.", exception.Message);60 }61 [Fact]62 public async Task SimpleDockerfile()63 {64 // Given65 var imageFromDockerfileBuilder = new ImageFromDockerfileBuilder()66 .WithName("alpine:custom")67 .WithDockerfileDirectory("Assets")68 .WithDeleteIfExists(true);69 // When70 var imageFromDockerfile1 = await imageFromDockerfileBuilder.Build();71 var imageFromDockerfile2 = await imageFromDockerfileBuilder.Build(); // Deletes the previously created image.72 // Then73 Assert.NotEmpty(imageFromDockerfile1);74 Assert.NotEmpty(imageFromDockerfile2);75 Assert.Equal(imageFromDockerfile1, imageFromDockerfile2);76 }77 }78}...
ManagerServiceFixture.cs
Source:ManagerServiceFixture.cs
...6 public IDockerImage BaseImage { get; } = new DockerImage("safir-common-dotnet-e2e");7 public IDockerImage Image { get; } = new DockerImage("safir-manager-e2e");8 public async Task InitializeAsync()9 {10 await new ImageFromDockerfileBuilder()11 .WithName(BaseImage)12 .WithDockerfile(Path.Combine("common", "dotnet", "Dockerfile"))13 .WithDockerfileDirectory(CommonDirectoryPath.GetGitDirectory(), "src")14 .Build();15 await new ImageFromDockerfileBuilder()16 .WithName(Image)17 .WithDockerfile("manager/Dockerfile")18 .WithDockerfileDirectory(CommonDirectoryPath.GetGitDirectory(), "src")19 .WithBuildArgument("CommonImage", BaseImage.FullName)20 .Build();21 }22 public Task DisposeAsync() => Task.CompletedTask;23}...
ImageFromDockerfileBuilder
Using AI Code Generation
1using DotNet.Testcontainers.Builders;2using DotNet.Testcontainers.Containers;3using DotNet.Testcontainers.Containers.Configurations.Databases;4using DotNet.Testcontainers.Containers.Modules;5using DotNet.Testcontainers.Containers.Modules.Databases;6using DotNet.Testcontainers.Containers.WaitStrategies;7using DotNet.Testcontainers.Images;8using System.Threading.Tasks;9{10 {11 static async Task Main(string[] args)12 {13 var image = new ImageFromDockerfileBuilder()14 .WithName("my-image")15 .WithDockerfile("Dockerfile")16 .Build();17 await image.StartAsync();18 }19 }20}
ImageFromDockerfileBuilder
Using AI Code Generation
1using DotNet.Testcontainers.Builders;2using DotNet.Testcontainers.Containers.Configurations.Databases;3using DotNet.Testcontainers.Containers.Modules.Databases;4using DotNet.Testcontainers.Containers.WaitStrategies;5using DotNet.Testcontainers.Images;6using System.Threading.Tasks;7{8 {9 static async Task Main(string[] args)10 {11 .Create("dotnet-testcontainers")12 .WithFileFromPath("Dockerfile", @"C:\Users\user\Documents\Visual Studio 2019\Projects\Testcontainers\Testcontainers\Dockerfile")13 .Build();14 await image.StartAsync();15 var postgres = new TestcontainersContainer()16 .WithDatabase(new PostgreSqlTestcontainerConfiguration("postgres:12.1-alpine"))17 .WithEnvironment("POSTGRES_PASSWORD", "testcontainers")18 .WithEnvironment("POSTGRES_USER", "testcontainers")19 .WithEnvironment("POSTGRES_DB", "testcontainers")20 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(5432))21 .WithImage(image)22 .Build();23 await postgres.StartAsync();24 }25 }26}27using DotNet.Testcontainers.Builders;28using DotNet.Testcontainers.Containers.Configurations.Databases;29using DotNet.Testcontainers.Containers.Modules.Databases;30using DotNet.Testcontainers.Containers.WaitStrategies;31using DotNet.Testcontainers.Images;32using System.Threading.Tasks;33{34 {35 static async Task Main(string[] args)36 {37 .Create("dotnet-testcontainers")38 .WithFileFromPath("Dockerfile", @"C:\Users\user\Documents\Visual Studio 2019\Projects\Testcontainers\Testcontainers\Dockerfile")39 .Build();40 await image.StartAsync();41 var postgres = new TestcontainersContainer()42 .WithDatabase(new PostgreSqlTestcontainerConfiguration("postgres:12.1-alpine"))43 .WithEnvironment("POSTGRES_PASSWORD", "testcontainers")44 .WithEnvironment("POSTGRES_USER", "testcontainers")45 .WithEnvironment("POSTGRES_DB", "testcontainers")46 .WithWaitStrategy(Wait
ImageFromDockerfileBuilder
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Builders;4using DotNet.Testcontainers.Containers;5using DotNet.Testcontainers.Containers.Builders;6using DotNet.Testcontainers.Containers.Modules;7using DotNet.Testcontainers.Containers.WaitStrategies;8using DotNet.Testcontainers.Images;9using DotNet.Testcontainers.Images.Builders;10using DotNet.Testcontainers.Images.Configurations;11{12 {13 static async Task Main(string[] args)14 {15 var image = await new ImageFromDockerfileBuilder()16 .WithDockerfile("Dockerfile")17 .WithBuildArg("DOTNET_SDK_VERSION", "5.0.100-rc.1.20452.10")18 .WithBuildArg("DOTNET_RUNTIME_VERSION", "5.0.0-rc.1.20451.14")19 .Build();20 var container = new TestcontainersBuilder<TestcontainersContainer>()21 .WithImage(image)22 .WithCleanUp(true)23 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80))24 .Build();25 await container.StartAsync();26 }27 }28}
ImageFromDockerfileBuilder
Using AI Code Generation
1using DotNet.Testcontainers.Builders;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 var image = await new ImageFromDockerfileBuilder()8 .WithDockerfile("Dockerfile")9 .Build();10 }11 }12}13using DotNet.Testcontainers.Builders;14using System.Threading.Tasks;15{16 {17 static async Task Main(string[] args)18 {19 var image = await new ImageFromDockerfileBuilder()20 .WithDockerfile("Dockerfile")21 .Build();22 }23 }24}25using DotNet.Testcontainers.Builders;26using System.Threading.Tasks;27{28 {29 static async Task Main(string[] args)30 {31 var image = await new ImageFromDockerfileBuilder()32 .WithDockerfile("Dockerfile")33 .Build();34 }35 }36}37using DotNet.Testcontainers.Builders;38using System.Threading.Tasks;39{40 {41 static async Task Main(string[] args)42 {43 var image = await new ImageFromDockerfileBuilder()44 .WithDockerfile("Dockerfile")45 .Build();46 }47 }48}49using DotNet.Testcontainers.Builders;50using System.Threading.Tasks;51{52 {53 static async Task Main(string[] args)54 {55 var image = await new ImageFromDockerfileBuilder()56 .WithDockerfile("Dockerfile")57 .Build();58 }59 }60}
ImageFromDockerfileBuilder
Using AI Code Generation
1ImageFromDockerfileBuilder imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();2 .WithDockerfile(Dockerfile.Create("FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build"))3 .Build();4ImageFromDockerfileBuilder imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();5 .WithDockerfile(Dockerfile.Create("FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build"))6 .Build();7ImageFromDockerfileBuilder imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();8 .WithDockerfile(Dockerfile.Create("FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build", "Dockerfile.custom"))9 .Build();10ImageFromDockerfileBuilder imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();11 .WithDockerfile(Dockerfile.Create("FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build", "Dockerfile.custom"))12 .Build();
ImageFromDockerfileBuilder
Using AI Code Generation
1var imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();2 .WithName("dotnet-testcontainers")3 .WithDockerfile("Dockerfile")4 .Build();5var dockerImage = await imageFromDockerfile.BuildAsync();6var imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();7 .WithName("dotnet-testcontainers")8 .WithDockerfile("Dockerfile")9 .Build();10var dockerImage = await imageFromDockerfile.BuildAsync();11var imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();12 .WithName("dotnet-testcontainers")13 .WithDockerfile("Dockerfile")14 .Build();15var dockerImage = await imageFromDockerfile.BuildAsync();16var imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();17 .WithName("dotnet-testcontainers")18 .WithDockerfile("Dockerfile")19 .Build();20var dockerImage = await imageFromDockerfile.BuildAsync();21var imageFromDockerfileBuilder = new ImageFromDockerfileBuilder();22 .WithName("dotnet-testcontainers")23 .WithDockerfile("Dockerfile")24 .Build();25var dockerImage = await imageFromDockerfile.BuildAsync();
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!!