Skip to content

Commit 3e99482

Browse files
committed
Javadoc
1 parent 2924a7b commit 3e99482

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

src/main/java/org/apache/commons/lang3/RuntimeEnvironment.java

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,71 @@ private static boolean fileExists(final String path) {
3636

3737
/**
3838
* Tests whether we are running in a container like Docker or Podman.
39+
* <p>
40+
* <em>The following may change if we find better detection logic.</em>
41+
* </p>
42+
* <p>
43+
* We roughly follow the logic in SystemD:
44+
* </p>
45+
* <p>
46+
* <a href=
47+
* "https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692">https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692</a>
48+
* </p>
49+
* <p>
50+
* We check the `container` environment variable of process 1:
51+
* </p>
52+
* <ol>
53+
* <li>If the variable is empty, we return false. This includes the case, where the container developer wants to hide the fact that the application runs in
54+
* a container.</li>
55+
* <li>If the variable is not empty, we return true.</li>
56+
* <li>If the variable is absent, we continue.</li>
57+
* <li>We check files in the container. According to SystemD:/
58+
* <ol>
59+
* <li>/.dockerenv is used by Docker.</li>
60+
* <li>/run/.containerenv is used by PodMan.</li>
61+
* </ol>
62+
* </li>
63+
* </ol>
3964
*
40-
* @return whether we are running in a container like Docker or Podman. Never null
65+
* @return whether we are running in a container like Docker or Podman. Never null.
66+
* @see <a href="https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692">SystemD virt.c</a>
4167
*/
4268
public static Boolean inContainer() {
4369
return inContainer(StringUtils.EMPTY);
4470
}
4571

72+
/**
73+
* Tests whether we are running in a container like Docker or Podman.
74+
* <p>
75+
* <em>The following may change if we find better detection logic.</em>
76+
* </p>
77+
* <p>
78+
* We roughly follow the logic in SystemD:
79+
* </p>
80+
* <p>
81+
* <a href=
82+
* "https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692">https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692</a>
83+
* </p>
84+
* <p>
85+
* We check the `container` environment variable of process 1:
86+
* </p>
87+
* <ol>
88+
* <li>If the variable is empty, we return false. This includes the case, where the container developer wants to hide the fact that the application runs in
89+
* a container.</li>
90+
* <li>If the variable is not empty, we return true.</li>
91+
* <li>If the variable is absent, we continue.</li>
92+
* <li>We check files in the container. According to SystemD:/
93+
* <ol>
94+
* <li>/.dockerenv is used by Docker.</li>
95+
* <li>/run/.containerenv is used by PodMan.</li>
96+
* </ol>
97+
* </li>
98+
* </ol>
99+
*
100+
* @return Whether we are running in a container like Docker or Podman.
101+
* @see <a href="https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692">SystemD virt.c</a>
102+
*/
46103
static boolean inContainer(final String dirPrefix) {
47-
/*
48-
Roughly follow the logic in SystemD:
49-
https://github.com/systemd/systemd/blob/0747e3b60eb4496ee122066c844210ce818d76d9/src/basic/virt.c#L692
50-
51-
We check the `container` environment variable of process 1:
52-
If the variable is empty, we return false. This includes the case, where the container developer wants to hide the fact that the application runs in a container.
53-
If the variable is not empty, we return true.
54-
If the variable is absent, we continue.
55-
56-
We check files in the container. According to SystemD:
57-
/.dockerenv is used by Docker.
58-
/run/.containerenv is used by PodMan.
59-
60-
*/
61104
final String value = readFile(dirPrefix + "/proc/1/environ", "container");
62105
if (value != null) {
63106
return !value.isEmpty();
@@ -79,12 +122,14 @@ private static String readFile(final String envVarFile, final String key) {
79122
// Split by null byte character
80123
final String[] lines = content.split(String.valueOf(CharUtils.NUL));
81124
final String prefix = key + "=";
125+
// @formatter:off
82126
return Arrays.stream(lines)
83127
.filter(line -> line.startsWith(prefix))
84128
.map(line -> line.split("=", 2))
85129
.map(keyValue -> keyValue[1])
86130
.findFirst()
87131
.orElse(null);
132+
// @formatter:on
88133
} catch (final IOException e) {
89134
return null;
90135
}

0 commit comments

Comments
 (0)