Max Strobel Maximilian Strobel glenn-jocher commited on
Commit
0b5ac22
1 Parent(s): c775a29

fix: broken ``is_docker`` check (#8711)

Browse files

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>

Files changed (1) hide show
  1. 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
- # Is environment a Docker container?
229
- return Path('/workspace').exists() # or Path('/.dockerenv').exists()
 
 
 
 
 
 
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():