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 <Maximilian.Strobel@infineon.com> Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
1 file changed
+9
-3
+9
-3
@@ -224,9 +224,15 @@ def get_latest_run(search_dir='.'):
|
|||||||
return max(last_list, key=os.path.getctime) if last_list else ''
|
return max(last_list, key=os.path.getctime) if last_list else ''
|
||||||
|
|
||||||
|
|
||||||
def is_docker():
|
def is_docker() -> bool:
|
||||||
# Is environment a Docker container?
|
"""Check if the process runs inside a docker container."""
|
||||||
return Path('/workspace').exists() # or Path('/.dockerenv').exists()
|
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():
|
def is_colab():
|
||||||
|
|||||||
Reference in new issue
Block a user