Skip to content

Commit

Permalink
Fallback to x86 image if image pulling fails (#4290)
Browse files Browse the repository at this point in the history
* Fallback to x86 image if image pulling fails

* Add the fallback to `checkAndPullImage`
  • Loading branch information
bsideup committed Jul 19, 2021
1 parent b837bb1 commit 71314ab
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
18 changes: 13 additions & 5 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.ListImagesCmd;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.AccessMode;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.Version;
import com.github.dockerjava.api.model.Volume;
Expand Down Expand Up @@ -37,7 +37,6 @@
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -330,8 +329,17 @@ private boolean checkMountableFile() {
public void checkAndPullImage(DockerClient client, String image) {
try {
client.inspectImageCmd(image).exec();
} catch (NotFoundException e) {
client.pullImageCmd(image).exec(new TimeLimitedLoggedPullImageResultCallback(log)).awaitCompletion();
} catch (NotFoundException notFoundException) {
PullImageCmd pullImageCmd = client.pullImageCmd(image);
try {
pullImageCmd.exec(new TimeLimitedLoggedPullImageResultCallback(log)).awaitCompletion();
} catch (DockerClientException e) {
// Try to fallback to x86
pullImageCmd
.withPlatform("linux/amd64")
.exec(new TimeLimitedLoggedPullImageResultCallback(log))
.awaitCompletion();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public static DockerClient getClientForConfig(TransportConfig transportConfig) {
DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder();

if (configBuilder.build().getApiVersion() == RemoteApiVersion.UNKNOWN_VERSION) {
configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_30);
configBuilder.withApiVersion(RemoteApiVersion.VERSION_1_32);
}
return DockerClientImpl.getInstance(
new AuthDelegatingDockerClientConfig(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testcontainers.images;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.PullImageCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import com.google.common.util.concurrent.Futures;
Expand Down Expand Up @@ -75,11 +76,21 @@ protected final String resolve() {

while (Instant.now().isBefore(lastRetryAllowed)) {
try {
dockerClient
PullImageCmd pullImageCmd = dockerClient
.pullImageCmd(imageName.getUnversionedPart())
.withTag(imageName.getVersionPart())
.exec(new TimeLimitedLoggedPullImageResultCallback(logger))
.awaitCompletion();
.withTag(imageName.getVersionPart());

try {
pullImageCmd
.exec(new TimeLimitedLoggedPullImageResultCallback(logger))
.awaitCompletion();
} catch (DockerClientException e) {
// Try to fallback to x86
pullImageCmd
.withPlatform("linux/amd64")
.exec(new TimeLimitedLoggedPullImageResultCallback(logger))
.awaitCompletion();
}

LocalImagesCache.INSTANCE.refreshCache(imageName);

Expand Down
12 changes: 6 additions & 6 deletions docs/supported_docker_environment/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

| Host Operating System / Environment | Minimum recommended docker versions | Known issues / tips |
|-------------------------------------|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Linux - general | Docker v1.10 | |
| 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 |
| 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) |
| 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 |
| Linux - general | Docker v17.09 | |
| 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 |
| Linux - CircleCI (LXC driver) | Docker v17.09 | The `exec` feature is not compatible with CircleCI. See CircleCI configuration [example](./continuous_integration/circle_ci.md) |
| 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 |
| Mac OS X - Docker Toolbox | Docker Machine v0.8.0 | |
| 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. |
| 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. |
| Windows - Docker Toolbox | | *Support is limited at present and this is not currently tested on a regular basis*. |
| Windows - Docker for Windows | | *Support is best-efforts at present.* Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md) |
| 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). |
| 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). |

## Docker environment discovery

Expand Down

0 comments on commit 71314ab

Please sign in to comment.