summaryrefslogtreecommitdiffstats
path: root/pre_commit/languages/docker.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-08-12 12:03:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-08-12 12:03:24 +0000
commitdb201c7e8329ebc584fbebcc18f5b64825f6d023 (patch)
treeeed388903a58a2988bdf587ee7c7622d167226f7 /pre_commit/languages/docker.py
parentAdding upstream version 2.13.0. (diff)
downloadpre-commit-db201c7e8329ebc584fbebcc18f5b64825f6d023.tar.xz
pre-commit-db201c7e8329ebc584fbebcc18f5b64825f6d023.zip
Adding upstream version 2.14.0.upstream/2.14.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pre_commit/languages/docker.py')
-rw-r--r--pre_commit/languages/docker.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/pre_commit/languages/docker.py b/pre_commit/languages/docker.py
index 5b21ec9..644d8d2 100644
--- a/pre_commit/languages/docker.py
+++ b/pre_commit/languages/docker.py
@@ -1,7 +1,6 @@
import hashlib
import json
import os
-import socket
from typing import Sequence
from typing import Tuple
@@ -9,6 +8,7 @@ import pre_commit.constants as C
from pre_commit.hook import Hook
from pre_commit.languages import helpers
from pre_commit.prefix import Prefix
+from pre_commit.util import CalledProcessError
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output_b
@@ -26,12 +26,28 @@ def _is_in_docker() -> bool:
return False
+def _get_container_id() -> str:
+ # It's assumed that we already check /proc/1/cgroup in _is_in_docker. The
+ # cpuset cgroup controller existed since cgroups were introduced so this
+ # way of getting the container ID is pretty reliable.
+ with open('/proc/1/cgroup', 'rb') as f:
+ for line in f.readlines():
+ if line.split(b':')[1] == b'cpuset':
+ return os.path.basename(line.split(b':')[2]).strip().decode()
+ raise RuntimeError('Failed to find the container ID in /proc/1/cgroup.')
+
+
def _get_docker_path(path: str) -> str:
if not _is_in_docker():
return path
- hostname = socket.gethostname()
- _, out, _ = cmd_output_b('docker', 'inspect', hostname)
+ container_id = _get_container_id()
+
+ try:
+ _, out, _ = cmd_output_b('docker', 'inspect', container_id)
+ except CalledProcessError:
+ # self-container was not visible from here (perhaps docker-in-docker)
+ return path
container, = json.loads(out)
for mount in container['Mounts']: