Skip to content

Commit

Permalink
fix(#666): DockerImageNotFoundException when logged in with Docker De…
Browse files Browse the repository at this point in the history
…sktop instead of the CLI (#677)
  • Loading branch information
HofmeisterAn committed Nov 16, 2022
1 parent edc5d0f commit 446f002
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

- 610 Trim traling slashes in Dockerfile directory path (otherwise, it cuts the first character of the relative path), Normalize paths to forward slashes
- 650 Update SharpZipLib to version 1.4.1 to prevent a deadlock in the Docker container image build
- 666 DockerImageNotFoundException when logged in with Docker Desktop instead of the CLI

## [2.2.0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// <inheritdoc cref="IDockerRegistryAuthenticationProvider" />
internal sealed class DockerRegistryAuthenticationProvider : IDockerRegistryAuthenticationProvider
{
private const string DockerHub = "index.docker.io";
private const string DockerHub = "https://index.docker.io/v1/";

private static readonly ConcurrentDictionary<string, Lazy<IDockerRegistryAuthenticationConfiguration>> Credentials = new ConcurrentDictionary<string, Lazy<IDockerRegistryAuthenticationConfiguration>>();

Expand Down
12 changes: 9 additions & 3 deletions src/Testcontainers/Clients/DockerSystemOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ public DockerSystemOperations(Guid sessionId, IDockerEndpointAuthenticationConfi

public async Task<bool> GetIsWindowsEngineEnabled(CancellationToken ct = default)
{
return (await this.Docker.System.GetSystemInfoAsync(ct)
.ConfigureAwait(false)).OperatingSystem.Contains("Windows");
var version = await this.GetVersionAsync(ct)
.ConfigureAwait(false);
return version.Os.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) > -1;
}

public Task<VersionResponse> GetVersion(CancellationToken ct = default)
public Task<SystemInfoResponse> GetInfoAsync(CancellationToken ct = default)
{
return this.Docker.System.GetSystemInfoAsync(ct);
}

public Task<VersionResponse> GetVersionAsync(CancellationToken ct = default)
{
return this.Docker.System.GetVersionAsync(ct);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Testcontainers/Clients/IDockerSystemOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ internal interface IDockerSystemOperations
{
Task<bool> GetIsWindowsEngineEnabled(CancellationToken ct = default);

Task<VersionResponse> GetVersion(CancellationToken ct = default);
Task<SystemInfoResponse> GetInfoAsync(CancellationToken ct = default);

Task<VersionResponse> GetVersionAsync(CancellationToken ct = default);
}
}
40 changes: 25 additions & 15 deletions src/Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Task<(string Stdout, string Stderr)> GetContainerLogs(string id, DateTime
public async Task StartAsync(string id, CancellationToken ct = default)
{
if (await this.containers.ExistsWithIdAsync(id, ct)
.ConfigureAwait(false))
.ConfigureAwait(false))
{
await this.containers.StartAsync(id, ct)
.ConfigureAwait(false);
Expand All @@ -138,7 +138,7 @@ await this.containers.StartAsync(id, ct)
public async Task StopAsync(string id, CancellationToken ct = default)
{
if (await this.containers.ExistsWithIdAsync(id, ct)
.ConfigureAwait(false))
.ConfigureAwait(false))
{
await this.containers.StopAsync(id, ct)
.ConfigureAwait(false);
Expand All @@ -149,7 +149,7 @@ await this.containers.StopAsync(id, ct)
public async Task RemoveAsync(string id, CancellationToken ct = default)
{
if (await this.containers.ExistsWithIdAsync(id, ct)
.ConfigureAwait(false))
.ConfigureAwait(false))
{
try
{
Expand Down Expand Up @@ -191,16 +191,17 @@ public async Task CopyFileAsync(string id, string filePath, byte[] fileContent,
using (var tarOutputStream = new TarOutputStream(memStream, Encoding.Default))
{
tarOutputStream.IsStreamOwner = false;
tarOutputStream.PutNextEntry(
new TarEntry(
new TarHeader
{
Name = containerPath,
UserId = userId,
GroupId = groupId,
Mode = accessMode,
Size = fileContent.Length,
}));

var header = new TarHeader();
header.Name = containerPath;
header.UserId = userId;
header.GroupId = groupId;
header.Mode = accessMode;
header.Size = fileContent.Length;

var entry = new TarEntry(header);

tarOutputStream.PutNextEntry(entry);

#if NETSTANDARD2_1_OR_GREATER
await tarOutputStream.WriteAsync(fileContent.AsMemory(0, fileContent.Length), ct)
Expand Down Expand Up @@ -284,8 +285,17 @@ await this.CopyFileAsync(containerId, resourceMapping.Target, resourceMappingCon

if (configuration.ImagePullPolicy(cachedImage))
{
var authConfig = default(DockerRegistryAuthenticationConfiguration).Equals(configuration.DockerRegistryAuthConfig)
? this.registryAuthenticationProvider.GetAuthConfig(configuration.Image.GetHostname()) : configuration.DockerRegistryAuthConfig;
var dockerRegistryServerAddress = configuration.Image.GetHostname();

if (dockerRegistryServerAddress == null)
{
var info = await this.system.GetInfoAsync(ct)
.ConfigureAwait(false);

dockerRegistryServerAddress = info.IndexServerAddress;
}

var authConfig = this.registryAuthenticationProvider.GetAuthConfig(dockerRegistryServerAddress);

await this.images.CreateAsync(configuration.Image, authConfig, ct)
.ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task GetVersionReturnsVersion()
IDockerSystemOperations dockerSystemOperations = new DockerSystemOperations(Guid.Empty, this.authConfig, NullLogger.Instance);

// When
var version = await dockerSystemOperations.GetVersion()
var version = await dockerSystemOperations.GetVersionAsync()
.ConfigureAwait(false);

// Then
Expand All @@ -58,7 +58,7 @@ public async Task GetVersionReturnsVersion()
IDockerSystemOperations dockerSystemOperations = new DockerSystemOperations(Guid.Empty, this.authConfig, NullLogger.Instance);

// When
var version = await dockerSystemOperations.GetVersion()
var version = await dockerSystemOperations.GetVersionAsync()
.ConfigureAwait(false);

// Then
Expand Down

0 comments on commit 446f002

Please sign in to comment.