is_docker/
lib.rs

1extern crate once_cell;
2
3use std::fs;
4use once_cell::sync::OnceCell;
5
6fn has_docker_env_file() -> bool {
7    fs::metadata("/.dockerenv").is_ok()
8}
9
10fn has_docker_in_cgroup() -> bool {
11    match fs::read_to_string("/proc/self/cgroup") {
12        Ok(file_contents) => file_contents.contains("docker"),
13        Err(_error) => false,
14    }
15}
16
17pub fn is_docker() -> bool {
18    static CACHED_RESULT: OnceCell<bool> = OnceCell::new();
19
20    *CACHED_RESULT.get_or_init(|| {
21        has_docker_env_file() || has_docker_in_cgroup()
22    })   
23}