Skip to content

Commit 1666ac2

Browse files
authored
feat: Support configuring Docker API version (#1576)
1 parent c8ff7dd commit 1666ac2

15 files changed

+101
-8
lines changed

docs/custom_configuration/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Testcontainers supports various configurations to set up your test environment.
44

55
| Properties File | Environment Variable | Description | Default |
66
|---------------------------------|------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|------------------------------|
7+
| `docker.api.version` | `DOCKER_API_VERSION` | The Docker API version to use. | `1.44` |
78
| `docker.config` | `DOCKER_CONFIG` | The directory path that contains the Docker configuration (`config.json`) file. | `~/.docker/` |
89
| `docker.host` | `DOCKER_HOST` | The Docker daemon socket to connect to. | - |
910
| `docker.context` | `DOCKER_CONTEXT` | The Docker context to connect to. | - |

src/Testcontainers/Builders/DockerDesktopEndpointAuthenticationProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public override bool IsApplicable()
2525
return !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && DockerEngine != null;
2626
}
2727

28+
/// <inheritdoc />
29+
public Version GetDockerApiVersion()
30+
{
31+
return null;
32+
}
33+
2834
/// <inheritdoc />
2935
public string GetDockerConfig()
3036
{

src/Testcontainers/Builders/DockerEndpointAuthenticationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public virtual bool IsAvailable()
4141
{
4242
using (var dockerClientConfiguration = authConfig.GetDockerClientConfiguration(ResourceReaper.DefaultSessionId))
4343
{
44-
using (var dockerClient = dockerClientConfiguration.CreateClient())
44+
using (var dockerClient = dockerClientConfiguration.CreateClient(authConfig.Version))
4545
{
4646
try
4747
{

src/Testcontainers/Builders/TestcontainersEndpointAuthenticationProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public override IDockerEndpointAuthenticationConfiguration GetAuthConfig()
5757
return new DockerEndpointAuthenticationConfiguration(_dockerEngine);
5858
}
5959

60+
/// <inheritdoc />
61+
public Version GetDockerApiVersion()
62+
{
63+
return _customConfiguration.GetDockerApiVersion();
64+
}
65+
6066
/// <inheritdoc />
6167
public string GetDockerConfig()
6268
{

src/Testcontainers/Clients/DockerApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private static IDockerClient GetDockerClient(Guid sessionId, IDockerEndpointAuth
134134
{
135135
using (var dockerClientConfiguration = dockerEndpointAuthConfig.GetDockerClientConfiguration(sessionId))
136136
{
137-
return dockerClientConfiguration.CreateClient();
137+
return dockerClientConfiguration.CreateClient(dockerEndpointAuthConfig.Version);
138138
}
139139
}
140140
}

src/Testcontainers/Configurations/AuthConfigs/DockerEndpointAuthenticationConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace DotNet.Testcontainers.Configurations
1010
[PublicAPI]
1111
public readonly struct DockerEndpointAuthenticationConfiguration : IDockerEndpointAuthenticationConfiguration
1212
{
13+
// https://github.com/moby/moby/releases/tag/docker-v29.0.0.
14+
private static readonly Version DockerEngineApi = EnvironmentConfiguration.Instance.GetDockerApiVersion() ?? PropertiesFileConfiguration.Instance.GetDockerApiVersion() ?? new Version(1, 44);
15+
1316
// Since the static `TestcontainersSettings` class holds the detected container
1417
// runtime information from the auto-discovery mechanism, we can't add a static
1518
// `NamedPipeConnectionTimeout` property to it because that would create a
@@ -30,6 +33,9 @@ public DockerEndpointAuthenticationConfiguration(Uri endpoint, Credentials crede
3033
Credentials = credentials;
3134
}
3235

36+
/// <inheritdoc />
37+
public Version Version => DockerEngineApi;
38+
3339
/// <inheritdoc />
3440
public Uri Endpoint { get; }
3541

src/Testcontainers/Configurations/AuthConfigs/IDockerEndpointAuthenticationConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ namespace DotNet.Testcontainers.Configurations
1010
[PublicAPI]
1111
public interface IDockerEndpointAuthenticationConfiguration
1212
{
13+
/// <summary>
14+
/// Gets the Docker API version.
15+
/// </summary>
16+
[CanBeNull]
17+
Version Version { get; }
18+
1319
/// <summary>
1420
/// Gets the Docker API endpoint.
1521
/// </summary>

src/Testcontainers/Configurations/CustomConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ protected CustomConfiguration(IReadOnlyDictionary<string, string> properties)
1515
_properties = properties;
1616
}
1717

18+
protected virtual Version GetDockerApiVersion(string propertyName)
19+
{
20+
return _properties.TryGetValue(propertyName, out var propertyValue) && !string.IsNullOrEmpty(propertyValue) && Version.TryParse(propertyValue, out var dockerApiVersion) ? dockerApiVersion : null;
21+
}
22+
1823
protected virtual string GetDockerConfig(string propertyName)
1924
{
2025
return GetPropertyValue<string>(propertyName);

src/Testcontainers/Configurations/EnvironmentConfiguration.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace DotNet.Testcontainers.Configurations
1010
/// </summary>
1111
internal class EnvironmentConfiguration : CustomConfiguration, ICustomConfiguration
1212
{
13+
private const string DockerApiVersion = "DOCKER_API_VERSION";
14+
1315
private const string DockerConfig = "DOCKER_CONFIG";
1416

1517
private const string DockerHost = "DOCKER_HOST";
@@ -54,11 +56,12 @@ static EnvironmentConfiguration()
5456
public EnvironmentConfiguration()
5557
: base(new[]
5658
{
57-
DockerAuthConfig,
58-
DockerCertPath,
59+
DockerApiVersion,
5960
DockerConfig,
6061
DockerHost,
6162
DockerContext,
63+
DockerAuthConfig,
64+
DockerCertPath,
6265
DockerTls,
6366
DockerTlsVerify,
6467
DockerHostOverride,
@@ -82,6 +85,12 @@ public EnvironmentConfiguration()
8285
public static ICustomConfiguration Instance { get; }
8386
= new EnvironmentConfiguration();
8487

88+
/// <inheritdoc />
89+
public Version GetDockerApiVersion()
90+
{
91+
return GetDockerApiVersion(DockerApiVersion);
92+
}
93+
8594
/// <inheritdoc />
8695
public string GetDockerConfig()
8796
{

src/Testcontainers/Configurations/ICustomConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ namespace DotNet.Testcontainers.Configurations
1010
/// </summary>
1111
internal interface ICustomConfiguration
1212
{
13+
/// <summary>
14+
/// Gets the Docker API version custom configuration.
15+
/// </summary>
16+
/// <returns>The Docker API version custom configuration.</returns>
17+
/// <remarks>https://dotnet.testcontainers.org/custom_configuration/.</remarks>
18+
[CanBeNull]
19+
Version GetDockerApiVersion();
20+
1321
/// <summary>
1422
/// Gets the Docker config custom configuration.
1523
/// </summary>

0 commit comments

Comments
 (0)