1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::fs;
use once_cell::sync::Lazy;
fn has_docker_env() -> bool {
fs::metadata("/.dockerenv").is_ok()
}
fn has_container_env() -> bool {
fs::metadata("/run/.containerenv").is_ok()
}
fn has_cgroup_v1() -> bool {
fs::read_to_string("/proc/1/cgroup").map_or(false, |contents| {
contents.contains("docker") || contents.contains("lxc")
})
}
fn has_mountinfo() -> bool {
fs::read_to_string("/proc/1/mountinfo").map_or(false, |contents| {
contents.contains("docker") || contents.contains("lxc")
})
}
pub fn is_container() -> bool {
static CACHED_RESULT: Lazy<bool> =
Lazy::new(|| has_docker_env() || has_container_env() || has_mountinfo() || has_cgroup_v1());
*CACHED_RESULT
}