Fallback to finding docker image name from compose ps

Earlier versions of docker do not include the image name in the inspect
output. We now fallback to using the `compose ps` response.

Closes gh-34993
This commit is contained in:
Phillip Webb 2023-04-15 18:37:25 -07:00
parent 24d1620a93
commit efff253d35
2 changed files with 33 additions and 20 deletions

View File

@ -45,11 +45,12 @@ class DefaultRunningService implements RunningService, OriginProvider {
private DockerEnv env;
DefaultRunningService(DockerHost host, DockerComposeFile composeFile, DockerCliComposePsResponse psResponse,
DefaultRunningService(DockerHost host, DockerComposeFile composeFile, DockerCliComposePsResponse composePsResponse,
DockerCliInspectResponse inspectResponse) {
this.origin = new DockerComposeOrigin(composeFile, psResponse.name());
this.name = psResponse.name();
this.image = ImageReference.of(psResponse.image());
this.origin = new DockerComposeOrigin(composeFile, composePsResponse.name());
this.name = composePsResponse.name();
this.image = ImageReference
.of((composePsResponse.image() != null) ? composePsResponse.image() : inspectResponse.config().image());
this.host = host;
this.ports = new DefaultConnectionPorts(inspectResponse);
this.env = new DockerEnv(inspectResponse.config().env());

View File

@ -56,22 +56,7 @@ class DefaultRunningServiceTests {
@BeforeEach
void setup() throws Exception {
this.composeFile = createComposeFile();
DockerHost host = DockerHost.get("192.168.1.1", () -> Collections.emptyList());
String id = "123";
String name = "my-service";
String image = "redis";
String state = "running";
DockerCliComposePsResponse psResponse = new DockerCliComposePsResponse(id, name, image, state);
Map<String, String> labels = Map.of("spring", "boot");
Map<String, ExposedPort> exposedPorts = Map.of("8080/tcp", new ExposedPort());
List<String> env = List.of("a=b");
Config config = new Config(image, labels, exposedPorts, env);
Map<String, List<HostPort>> ports = Map.of("8080/tcp", List.of(new HostPort(null, "9090")));
NetworkSettings networkSettings = new NetworkSettings(ports);
HostConfig hostConfig = new HostConfig("bridge");
DockerCliInspectResponse inspectResponse = new DockerCliInspectResponse(id, config, networkSettings,
hostConfig);
this.runningService = new DefaultRunningService(host, this.composeFile, psResponse, inspectResponse);
this.runningService = createRunningService(true);
}
private DockerComposeFile createComposeFile() throws IOException {
@ -95,6 +80,13 @@ class DefaultRunningServiceTests {
assertThat(this.runningService.image()).hasToString("redis");
}
@Test // gh-34992
void imageWhenUsingEarlierDockerVersionReturnsImageFromInspectResult() {
DefaultRunningService runningService = createRunningService(false);
assertThat(runningService.image()).hasToString("redis");
}
@Test
void hostReturnsHost() {
assertThat(this.runningService.host()).isEqualTo("192.168.1.1");
@ -122,4 +114,24 @@ class DefaultRunningServiceTests {
assertThat(this.runningService).hasToString("my-service");
}
private DefaultRunningService createRunningService(boolean psResponseHasImage) {
DockerHost host = DockerHost.get("192.168.1.1", () -> Collections.emptyList());
String id = "123";
String name = "my-service";
String image = "redis";
String state = "running";
DockerCliComposePsResponse psResponse = new DockerCliComposePsResponse(id, name,
(!psResponseHasImage) ? null : image, state);
Map<String, String> labels = Map.of("spring", "boot");
Map<String, ExposedPort> exposedPorts = Map.of("8080/tcp", new ExposedPort());
List<String> env = List.of("a=b");
Config config = new Config(image, labels, exposedPorts, env);
Map<String, List<HostPort>> ports = Map.of("8080/tcp", List.of(new HostPort(null, "9090")));
NetworkSettings networkSettings = new NetworkSettings(ports);
HostConfig hostConfig = new HostConfig("bridge");
DockerCliInspectResponse inspectResponse = new DockerCliInspectResponse(id, config, networkSettings,
hostConfig);
return new DefaultRunningService(host, this.composeFile, psResponse, inspectResponse);
}
}