File tree Expand file tree Collapse file tree 4 files changed +40
-0
lines changed
main/java/io/avaje/config Expand file tree Collapse file tree 4 files changed +40
-0
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 77import java .time .Duration ;
88import java .time .LocalDate ;
99import java .util .Map ;
10+ import java .util .Optional ;
1011import java .util .Properties ;
1112import java .util .concurrent .atomic .AtomicReference ;
1213
@@ -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" );
Original file line number Diff line number Diff line change 11myapp :
2+ testHost : ${docker.host}
23 activateFoo : true
34 fooName : Hello
45 fooHome : ${HOME}/config
You can’t perform that action at this time.
0 commit comments