From bfe1bd89f4aa9fad29a5f22712171fa52ff8f710 Mon Sep 17 00:00:00 2001 From: Max Strobel Date: Tue, 26 Jul 2022 18:02:44 +0100 Subject: [PATCH] fix: broken ``is_docker`` check (#8711) Checking if ``/workspace`` exists is not a reliable method to check if the process runs in a docker container. Reusing the logic from the npm "is-docker" package to check if the process runs in a container. References: https://github.com/sindresorhus/is-docker/blob/main/index.js Fixes #8710. Co-authored-by: Maximilian Strobel Co-authored-by: Glenn Jocher --- utils/general.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils/general.py b/utils/general.py index b049ce469a71..67078338d762 100755 --- a/utils/general.py +++ b/utils/general.py @@ -224,9 +224,15 @@ def get_latest_run(search_dir='.'): return max(last_list, key=os.path.getctime) if last_list else '' -def is_docker(): - # Is environment a Docker container? - return Path('/workspace').exists() # or Path('/.dockerenv').exists() +def is_docker() -> bool: + """Check if the process runs inside a docker container.""" + if Path("/.dockerenv").exists(): + return True + try: # check if docker is in control groups + with open("/proc/self/cgroup") as file: + return any("docker" in line for line in file) + except OSError: + return False def is_colab():