Skip to content

Commit 5dda334

Browse files
authored
Merge pull request #110 from avaje/feature/dockerHost-detection
Add automatic detection of ${docker.host}
2 parents d2d75d3 + 7a04943 commit 5dda334

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

avaje-config/src/main/java/io/avaje/config/CoreExpressionEval.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class CoreExpressionEval implements Configuration.ExpressionEval {
1818
* Used to detect the end of an expression.
1919
*/
2020
private static final String END = "}";
21+
private static final String DOCKER_HOST = "docker.host";
2122

2223
private CoreEntry.CoreMap sourceMap;
2324
private Properties sourceProperties;
@@ -148,6 +149,10 @@ void evaluate() {
148149
} else {
149150
if (defaultValue != null) {
150151
buf.append(defaultValue);
152+
} else if (DOCKER_HOST.equals(expression)) {
153+
final String dockerHost = DockerHost.host();
154+
buf.append(dockerHost);
155+
System.setProperty("docker.host", dockerHost);
151156
} else {
152157
buf.append(START).append(expression).append(END);
153158
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.avaje.config;
2+
3+
import java.io.File;
4+
import java.util.Locale;
5+
6+
/**
7+
* Support automatic translation of ${docker.host} detecting Docker-In-Docker and OS.
8+
*/
9+
final class DockerHost {
10+
11+
static String host() {
12+
return !dind() ? "localhost" : dockerInDockerHost();
13+
}
14+
15+
private static boolean dind() {
16+
return (new File("/.dockerenv")).exists();
17+
}
18+
19+
private static String dockerInDockerHost() {
20+
String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
21+
return !os.contains("mac") && !os.contains("darwin") && !os.contains("win") ? "172.17.0.1" : "host.docker.internal";
22+
}
23+
24+
}

avaje-config/src/test/java/io/avaje/config/ConfigTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.time.Duration;
88
import java.time.LocalDate;
99
import java.util.Map;
10+
import java.util.Optional;
1011
import java.util.Properties;
1112
import java.util.concurrent.atomic.AtomicReference;
1213

@@ -125,7 +126,7 @@ void asProperties() {
125126
assertThat(System.getProperty("myapp.bar.barDouble")).isEqualTo("33.3");
126127

127128
assertThat(properties).containsKeys("config.load.systemProperties", "config.watch.enabled", "myExternalLoader", "myapp.activateFoo", "myapp.bar.barDouble", "myapp.bar.barRules", "myapp.fooHome", "myapp.fooName", "system.excluded.properties");
128-
assertThat(properties).hasSize(11);
129+
assertThat(properties).hasSize(12);
129130
}
130131

131132
@Test
@@ -176,6 +177,15 @@ void get_withEval() {
176177
assertThat(Config.get("myapp.fooHome")).isEqualTo(home + "/config");
177178
}
178179

180+
@Test
181+
void get_dockerHost() {
182+
Optional<String> dockerHost = Config.getOptional("myapp.testHost");
183+
assertThat(dockerHost).isPresent();
184+
String testHost = dockerHost.get();
185+
assertThat(testHost).isNotEqualTo("${docker.host}");
186+
assertThat(System.getProperty("docker.host")).isEqualTo(testHost);
187+
}
188+
179189
@Test
180190
public void get_default() {
181191
assertThat(Config.get("myapp.doesNotExist2", "MyDefault")).isEqualTo("MyDefault");

avaje-config/src/test/resources/application-test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
myapp:
2+
testHost: ${docker.host}
23
activateFoo: true
34
fooName: Hello
45
fooHome: ${HOME}/config

0 commit comments

Comments
 (0)