Welcome to Testcontainers for .NET!
dotnet add package Testcontainers
// Create a new instance of a container.
var container = new ContainerBuilder()
// Set the image for the container to "testcontainers/helloworld:1.1.0".
.WithImage("testcontainers/helloworld:1.1.0")
// Bind port 8080 of the container to a random port on the host.
.WithPortBinding(8080, true)
// Wait until the HTTP endpoint of the container is available.
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(8080)))
// Build the container configuration.
.Build();
// Start the container.
await container.StartAsync()
.ConfigureAwait(false);
// Create a new instance of HttpClient to send HTTP requests.
var httpClient = new HttpClient();
// Construct the request URI by specifying the scheme, hostname, assigned random host port, and the endpoint "uuid".
var requestUri = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPort(8080), "uuid").Uri;
// Send an HTTP GET request to the specified URI and retrieve the response as a string.
var guid = await httpClient.GetStringAsync(requestUri)
.ConfigureAwait(false);
// Ensure that the retrieved UUID is a valid GUID.
Debug.Assert(Guid.TryParse(guid, out _));
Not using .NET? Here are other supported languages!
About
Testcontainers for .NET is a library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions. The library is built on top of the .NET Docker remote API and provides a lightweight implementation to support your test environment in all circumstances.
Choose from existing pre-configured modules and start containers within a second, to support and run your tests. Or create your own container images with Dockerfiles and run your containers and tests immediately afterward.
For more detailed instructions and guidance, please refer to the Testcontainers' Getting Started guide. If you are specifically interested in using Testcontainers for .NET, you find a dedicated follow-up guide here. These resources will provide you with comprehensive information to help you get started and make the most out of Testcontainers.
Supported operating systems
Testcontainers supports Windows, Linux, and macOS as host systems. Linux Docker containers are supported on all three operating systems. Native Windows Docker containers are only supported on Windows. Windows requires the host operating system version to match the container operating system version. You will find further information about Windows container version compatibility here.
With Docker Desktop you can switch the engine either with the tray icon context menu or: $env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchDaemon
or -SwitchLinuxEngine
, -SwitchWindowsEngine
.
System requirements
Testcontainers requires a Docker-API compatible container runtime. During development, Testcontainers is actively tested against recent versions of Docker on Linux, as well as against Docker Desktop on Mac and Windows. These Docker environments are automatically detected and used by Testcontainers without any additional configuration being necessary.
It is possible to configure Testcontainers to work for other Docker setups, such as a remote Docker host or Docker alternatives. However, these are not actively tested in the main development workflow, so not all Testcontainers features might be available and additional manual configuration might be necessary. If you have further questions about configuration details for your setup or whether it supports running Testcontainers-based tests, please contact the Testcontainers team and other users from the Testcontainers community on Slack.
License
See LICENSE.
Copyright
Copyright (c) 2019 - 2024 Andre Hofmeister and other authors.
See contributors for all contributors.
Join our Slack workspace | Testcontainers OSS | Testcontainers Cloud