Kaynağa Gözat

changed is_running_in_docker for working version from stackoverflow

Markus Spring 1 yıl önce
ebeveyn
işleme
9356de7639
1 değiştirilmiş dosya ile 16 ekleme ve 7 silme
  1. 16 7
      apps/home/modules/utils.py

+ 16 - 7
apps/home/modules/utils.py

@@ -2,18 +2,27 @@
 utility functions for garmindb-mtb-dashboard
 """
 
-import sys
+# import sys
+
+from pathlib import Path
 
 def is_running_in_docker():
     """
     Checks if the current process is running inside a Docker container.
     """
-    try:
-        with open('/proc/1/cgroup', 'r') as f:
-            cgroup = f.read()
-            return 'docker' in cgroup
-    except FileNotFoundError:
-        return False
+    cgroup = Path('/proc/self/cgroup')
+    return Path('/.dockerenv').is_file() or cgroup.is_file() and 'docker' in cgroup.read_text()
+
+# def is_running_in_docker():
+#     """
+#     Checks if the current process is running inside a Docker container.
+#     """
+#     try:
+#         with open('/proc/1/cgroup', 'r') as f:
+#             cgroup = f.read()
+#             return 'docker' in cgroup
+#     except FileNotFoundError:
+#         return False
 
     # if is_running_in_docker():
     #     sys.stderr.write("Running in a Docker container\n")