Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcb63a5c

Browse files
authored
Merge pull request#3535 from br-rhrbacek/fix-cgroups
Fix docker-in-docker detection for cgroups v2
2 parents9143fc3 +f80801d commitcb63a5c

File tree

2 files changed

+201
-69
lines changed

2 files changed

+201
-69
lines changed

‎pre_commit/languages/docker.py‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__importannotations
22

3+
importcontextlib
34
importfunctools
45
importhashlib
56
importjson
67
importos
8+
importre
79
fromcollections.abcimportSequence
810

911
frompre_commitimportlang_base
@@ -17,31 +19,33 @@
1719
health_check=lang_base.basic_health_check
1820
in_env=lang_base.no_env# no special environment for docker
1921

22+
_HOSTNAME_MOUNT_RE=re.compile(
23+
rb"""
24+
/containers
25+
(?:/overlay-containers)?
26+
/([a-z0-9]{64})
27+
(?:/userdata)?
28+
/hostname
29+
""",
30+
re.VERBOSE,
31+
)
2032

21-
def_is_in_docker()->bool:
22-
try:
23-
withopen('/proc/1/cgroup','rb')asf:
24-
returnb'docker'inf.read()
25-
exceptFileNotFoundError:
26-
returnFalse
2733

34+
def_get_container_id()->str|None:
35+
withcontextlib.suppress(FileNotFoundError):
36+
withopen('/proc/1/mountinfo','rb')asf:
37+
forlineinf:
38+
m=_HOSTNAME_MOUNT_RE.search(line)
39+
ifm:
40+
returnm[1].decode()
2841

29-
def_get_container_id()->str:
30-
# It's assumed that we already check /proc/1/cgroup in _is_in_docker. The
31-
# cpuset cgroup controller existed since cgroups were introduced so this
32-
# way of getting the container ID is pretty reliable.
33-
withopen('/proc/1/cgroup','rb')asf:
34-
forlineinf.readlines():
35-
ifline.split(b':')[1]==b'cpuset':
36-
returnos.path.basename(line.split(b':')[2]).strip().decode()
37-
raiseRuntimeError('Failed to find the container ID in /proc/1/cgroup.')
42+
returnNone
3843

3944

4045
def_get_docker_path(path:str)->str:
41-
ifnot_is_in_docker():
42-
returnpath
43-
4446
container_id=_get_container_id()
47+
ifcontainer_idisNone:
48+
returnpath
4549

4650
try:
4751
_,out,_=cmd_output_b('docker','inspect',container_id)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp