我正在使用docker-maven-plugin
并且根据文档我需要通过<envPropertyFile>
. 所以 pom.xml 中的插件看起来像
<configuration>
<images>
<image>
<build>
...
</build>
<run>
<envPropertyFile>${project.basedir}/local/local.properties</envPropertyFile>
</run>
</image>
</images>
</configuration>
我有以下值的 local.properties,
TIME_COUNT=1000
REST=10
在我的 java 项目中,我将这些值读为
System.getenv("TIME_COUNT"); #which returns null.
故障排除:
1.当我检查容器内的环境时,我看到TIME_COUNT=1000 and REST=10
.
docker exec -it CONTAINER_ID bash
env
2.当我执行
docker inspect -f '{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' CONTAINER_ID
我看到所有 env 值(即 TIME_COUNT=1000, REST=10 )
3.在我的 java 中,当我尝试检索所有环境时,我没有从 local.properties 或我可以通过执行在容器内看到的默认值获得任何 env 值env
。
StringBuilder sb = new StringBuilder();
Map<String, String> env = System.getenv();
for (String key : env.keySet()) {
sb.append(key + ": " + env.get(key) + "\n");
}
System.out.println(sb.toString());
4.我还尝试传递如下所述的 env 变量,它覆盖了容器中的值,但 jar 文件仍然抛出 null。
docker exec -e TIME_COUNT=12 -it CONTAINER_ID bash