fix: broken ``is_docker`` check (#8711)
Browse filesChecking 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 <[email protected]>
Co-authored-by: Glenn Jocher <[email protected]>
- utils/general.py +9 -3
utils/general.py
CHANGED
@@ -224,9 +224,15 @@ def get_latest_run(search_dir='.'):
|
|
224 |
return max(last_list, key=os.path.getctime) if last_list else ''
|
225 |
|
226 |
|
227 |
-
def is_docker():
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
|
231 |
|
232 |
def is_colab():
|
|
|
224 |
return max(last_list, key=os.path.getctime) if last_list else ''
|
225 |
|
226 |
|
227 |
+
def is_docker() -> bool:
|
228 |
+
"""Check if the process runs inside a docker container."""
|
229 |
+
if Path("/.dockerenv").exists():
|
230 |
+
return True
|
231 |
+
try: # check if docker is in control groups
|
232 |
+
with open("/proc/self/cgroup") as file:
|
233 |
+
return any("docker" in line for line in file)
|
234 |
+
except OSError:
|
235 |
+
return False
|
236 |
|
237 |
|
238 |
def is_colab():
|