Skip to content

Commit 5e8cfc6

Browse files
committed
Mad Dockerfile more robust.
1 parent 6a7f1d2 commit 5e8cfc6

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/test/java/com/github/dockerjava/core/command/DockerfileFixture.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.dockerjava.core.command;
22

33
import com.github.dockerjava.api.DockerClient;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46

57
import java.io.File;
68

@@ -9,6 +11,7 @@
911
*/
1012
public class DockerfileFixture implements AutoCloseable {
1113

14+
private static final Logger LOGGER = LoggerFactory.getLogger(DockerfileFixture.class);
1215
private final DockerClient dockerClient;
1316
private String directory;
1417
private String repository;
@@ -21,6 +24,7 @@ public DockerfileFixture(DockerClient dockerClient, String directory) {
2124

2225
public void open() throws Exception {
2326

27+
LOGGER.info("building {}", directory);
2428
dockerClient
2529
.buildImageCmd(new File("src/test/resources", directory))
2630
.withNoCache() // remove alternatives, cause problems
@@ -33,11 +37,15 @@ public void open() throws Exception {
3337
.get(0)
3438
.getRepoTags()[0];
3539

40+
LOGGER.info("created {}", repository);
41+
3642
containerId = dockerClient
3743
.createContainerCmd(repository)
3844
.exec()
3945
.getId();
4046

47+
LOGGER.info("starting {}", containerId);
48+
4149
dockerClient
4250
.startContainerCmd(containerId)
4351
.exec();
@@ -46,15 +54,23 @@ public void open() throws Exception {
4654
@Override
4755
public void close() throws Exception {
4856

49-
dockerClient
50-
.removeContainerCmd(containerId)
51-
.withForce() // stop too
52-
.exec();
57+
if (containerId != null) {
58+
LOGGER.info("removing container {}", containerId);
59+
dockerClient
60+
.removeContainerCmd(containerId)
61+
.withForce() // stop too
62+
.exec();
63+
containerId = null;
64+
}
5365

54-
dockerClient
55-
.removeImageCmd(repository)
56-
.withForce()
57-
.exec();
66+
if (repository != null) {
67+
LOGGER.info("removing repostiory {}", repository);
68+
dockerClient
69+
.removeImageCmd(repository)
70+
.withForce()
71+
.exec();
72+
repository = null;
73+
}
5874
}
5975

6076
public String getContainerId() {

0 commit comments

Comments
 (0)