From fd59c8277cc36ec76826e2042511848f71825001 Mon Sep 17 00:00:00 2001 From: Maximilian Strobel Date: Mon, 25 Jul 2022 11:53:26 +0200 Subject: [PATCH] fix: broken ``is_docker`` check 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. --- 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():