Skip to content

Commit 37e63c8

Browse files
Yury Bubnovrnorth
authored andcommitted
added possibility to specify Docker config.json via environment variable, java property or by default location
1 parent 5774e24 commit 37e63c8

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ private Logger logger() {
190190
return LoggerFactory.getLogger(DockerComposeContainer.class);
191191
}
192192

193-
@Override @VisibleForTesting
193+
@Override
194+
@VisibleForTesting
194195
public void finished(Description description) {
195196

196197

@@ -227,7 +228,7 @@ public void finished(Description description) {
227228

228229
public SELF withExposedService(String serviceName, int servicePort) {
229230

230-
if (! serviceName.matches(".*_[0-9]+")) {
231+
if (!serviceName.matches(".*_[0-9]+")) {
231232
serviceName += "_1"; // implicit first instance of this service
232233
}
233234

@@ -362,7 +363,9 @@ default void validateFileList(List<File> composeFiles) {
362363
class ContainerisedDockerCompose extends GenericContainer<ContainerisedDockerCompose> implements DockerCompose {
363364

364365
private static final String DOCKER_SOCKET_PATH = "/var/run/docker.sock";
365-
private static final String DOCKER_CONFIG_PATH = "/root/.docker";
366+
private static final String DOCKER_CONFIG_FILE = "/root/.docker/config.json";
367+
private static final String DOCKER_CONFIG_ENV = "DOCKER_CONFIG_FILE";
368+
private static final String DOCKER_CONFIG_PROPERTY = "dockerConfigFile";
366369
public static final char UNIX_PATH_SEPERATOR = ':';
367370

368371
public ContainerisedDockerCompose(List<File> composeFiles, String identifier) {
@@ -378,10 +381,10 @@ public ContainerisedDockerCompose(List<File> composeFiles, String identifier) {
378381
final String containerPwd = MountableFile.forHostPath(pwd).getFilesystemPath();
379382

380383
final List<String> absoluteDockerComposeFiles = composeFiles.stream()
381-
.map(File::getAbsolutePath)
382-
.map(MountableFile::forHostPath)
383-
.map(MountableFile::getFilesystemPath)
384-
.collect(toList());
384+
.map(File::getAbsolutePath)
385+
.map(MountableFile::forHostPath)
386+
.map(MountableFile::getFilesystemPath)
387+
.collect(toList());
385388
final String composeFileEnvVariableValue = Joiner.on(UNIX_PATH_SEPERATOR).join(absoluteDockerComposeFiles); // we always need the UNIX path separator
386389
logger().debug("Set env COMPOSE_FILE={}", composeFileEnvVariableValue);
387390
addEnv(ENV_COMPOSE_FILE, composeFileEnvVariableValue);
@@ -395,9 +398,17 @@ public ContainerisedDockerCompose(List<File> composeFiles, String identifier) {
395398
addEnv("DOCKER_HOST", "unix:///docker.sock");
396399
setStartupCheckStrategy(new IndefiniteWaitOneShotStartupCheckStrategy());
397400
setWorkingDirectory(containerPwd);
398-
Path dockerConfig = Paths.get(System.getProperty("user.home"), ".docker");
399-
if (Files.exists(dockerConfig)){
400-
addFileSystemBind(dockerConfig.toString(), DOCKER_CONFIG_PATH, READ_ONLY);
401+
402+
String dockerConfigEnv = System.getenv(DOCKER_CONFIG_ENV);
403+
String dockerConfigProperty = System.getProperty(DOCKER_CONFIG_PROPERTY);
404+
Path dockerConfig = Paths.get(System.getProperty("user.home"), ".docker", "config.json");
405+
406+
if (dockerConfigEnv != null && !dockerConfigEnv.trim().isEmpty()) {
407+
addFileSystemBind(dockerConfigEnv.toString(), DOCKER_CONFIG_FILE, READ_ONLY);
408+
} else if (dockerConfigProperty != null && !dockerConfigProperty.trim().isEmpty()) {
409+
addFileSystemBind(dockerConfigProperty.toString(), DOCKER_CONFIG_FILE, READ_ONLY);
410+
} else if (Files.exists(dockerConfig)) {
411+
addFileSystemBind(dockerConfig.toString(), DOCKER_CONFIG_FILE, READ_ONLY);
401412
}
402413

403414
}

0 commit comments

Comments
 (0)