Skip to content

Commit 71314ab

Browse files
authored
Fallback to x86 image if image pulling fails (#4290)
* Fallback to x86 image if image pulling fails * Add the fallback to `checkAndPullImage`
1 parent b837bb1 commit 71314ab

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

core/src/main/java/org/testcontainers/DockerClientFactory.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import com.github.dockerjava.api.DockerClient;
44
import com.github.dockerjava.api.async.ResultCallback;
55
import com.github.dockerjava.api.command.CreateContainerCmd;
6-
import com.github.dockerjava.api.command.ListImagesCmd;
6+
import com.github.dockerjava.api.command.PullImageCmd;
7+
import com.github.dockerjava.api.exception.DockerClientException;
78
import com.github.dockerjava.api.exception.InternalServerErrorException;
89
import com.github.dockerjava.api.exception.NotFoundException;
910
import com.github.dockerjava.api.model.AccessMode;
1011
import com.github.dockerjava.api.model.Bind;
1112
import com.github.dockerjava.api.model.Frame;
12-
import com.github.dockerjava.api.model.Image;
1313
import com.github.dockerjava.api.model.Info;
1414
import com.github.dockerjava.api.model.Version;
1515
import com.github.dockerjava.api.model.Volume;
@@ -37,7 +37,6 @@
3737
import java.io.InputStream;
3838
import java.net.URI;
3939
import java.util.ArrayList;
40-
import java.util.Arrays;
4140
import java.util.List;
4241
import java.util.Map;
4342
import java.util.Optional;
@@ -330,8 +329,17 @@ private boolean checkMountableFile() {
330329
public void checkAndPullImage(DockerClient client, String image) {
331330
try {
332331
client.inspectImageCmd(image).exec();
333-
} catch (NotFoundException e) {
334-
client.pullImageCmd(image).exec(new TimeLimitedLoggedPullImageResultCallback(log)).awaitCompletion();
332+
} catch (NotFoundException notFoundException) {
333+
PullImageCmd pullImageCmd = client.pullImageCmd(image);
334+
try {
335+
pullImageCmd.exec(new TimeLimitedLoggedPullImageResultCallback(log)).awaitCompletion();
336+
} catch (DockerClientException e) {
337+
// Try to fallback to x86
338+
pullImageCmd
339+
.withPlatform("linux/amd64")
340+
.exec(new TimeLimitedLoggedPullImageResultCallback(log))
341+
.awaitCompletion();
342+
}
335343
}
336344
}
337345

core/src/main/java/org/testcontainers/dockerclient/DockerClientProviderStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public static DockerClient getClientForConfig(TransportConfig transportConfig) {
240240
DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder();
241241

242242
if (configBuilder.build().getApiVersion() == RemoteApiVersion.UNKNOWN_VERSION) {
243-
configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_30);
243+
configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_32);
244244
}
245245
return DockerClientImpl.getInstance(
246246
new AuthDelegatingDockerClientConfig(

core/src/main/java/org/testcontainers/images/RemoteDockerImage.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testcontainers.images;
22

33
import com.github.dockerjava.api.DockerClient;
4+
import com.github.dockerjava.api.command.PullImageCmd;
45
import com.github.dockerjava.api.exception.DockerClientException;
56
import com.github.dockerjava.api.exception.InternalServerErrorException;
67
import com.google.common.util.concurrent.Futures;
@@ -75,11 +76,21 @@ protected final String resolve() {
7576

7677
while (Instant.now().isBefore(lastRetryAllowed)) {
7778
try {
78-
dockerClient
79+
PullImageCmd pullImageCmd = dockerClient
7980
.pullImageCmd(imageName.getUnversionedPart())
80-
.withTag(imageName.getVersionPart())
81-
.exec(new TimeLimitedLoggedPullImageResultCallback(logger))
82-
.awaitCompletion();
81+
.withTag(imageName.getVersionPart());
82+
83+
try {
84+
pullImageCmd
85+
.exec(new TimeLimitedLoggedPullImageResultCallback(logger))
86+
.awaitCompletion();
87+
} catch (DockerClientException e) {
88+
// Try to fallback to x86
89+
pullImageCmd
90+
.withPlatform("linux/amd64")
91+
.exec(new TimeLimitedLoggedPullImageResultCallback(logger))
92+
.awaitCompletion();
93+
}
8394

8495
LocalImagesCache.INSTANCE.refreshCache(imageName);
8596

docs/supported_docker_environment/index.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
| Host Operating System / Environment | Minimum recommended docker versions | Known issues / tips |
66
|-------------------------------------|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
7-
| Linux - general | Docker v1.10 | |
8-
| Linux - Travis CI | Docker v1.10 | See [example .travis.yml](https://raw.githubusercontent.com/testcontainers/testcontainers-java/master/.travis.yml) for baseline Travis CI configuration |
9-
| Linux - CircleCI (LXC driver) | Docker v1.9.1 | The `exec` feature is not compatible with CircleCI. See CircleCI configuration [example](./continuous_integration/circle_ci.md) |
10-
| Linux - within a Docker container | Docker v1.12 | See [Running inside Docker](continuous_integration/dind_patterns.md) for Docker-in-Docker and Docker wormhole patterns |
7+
| Linux - general | Docker v17.09 | |
8+
| Linux - Travis CI | Docker v17.09 | See [example .travis.yml](https://raw.githubusercontent.com/testcontainers/testcontainers-java/master/.travis.yml) for baseline Travis CI configuration |
9+
| Linux - CircleCI (LXC driver) | Docker v17.09 | The `exec` feature is not compatible with CircleCI. See CircleCI configuration [example](./continuous_integration/circle_ci.md) |
10+
| Linux - within a Docker container | Docker v17.09 | See [Running inside Docker](continuous_integration/dind_patterns.md) for Docker-in-Docker and Docker wormhole patterns |
1111
| Mac OS X - Docker Toolbox | Docker Machine v0.8.0 | |
12-
| Mac OS X - Docker for Mac | 1.12.0 | *Support is best-efforts at present*. `getTestHostIpAddress()` is [not currently supported](https://github.com/testcontainers/testcontainers-java/issues/166) due to limitations in Docker for Mac. |
12+
| Mac OS X - Docker for Mac | v17.09 | *Support is best-efforts at present*. `getTestHostIpAddress()` is [not currently supported](https://github.com/testcontainers/testcontainers-java/issues/166) due to limitations in Docker for Mac. |
1313
| Windows - Docker Toolbox | | *Support is limited at present and this is not currently tested on a regular basis*. |
1414
| Windows - Docker for Windows | | *Support is best-efforts at present.* Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md) |
15-
| Windows - Windows Subsystem for Linux (WSL) | Docker v17.06 | *Support is best-efforts at present.* Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md). |
15+
| Windows - Windows Subsystem for Linux (WSL) | Docker v17.09 | *Support is best-efforts at present.* Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md). |
1616

1717
## Docker environment discovery
1818

0 commit comments

Comments
 (0)