Skip to content

Commit

Permalink
A more reliable way to get the container id.
Browse files Browse the repository at this point in the history
The hostname is not always the container id. Get the container id from
/proc/1/cgroup. Fixes #1918.
  • Loading branch information
adarnimrod committed May 22, 2021
1 parent 229a4e0 commit 627209d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions 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

Expand Down Expand Up @@ -29,9 +28,11 @@ def _is_in_docker() -> bool:
def _get_docker_path(path: str) -> str:
if not _is_in_docker():
return path
hostname = socket.gethostname()
container_id = os.path.basename(
open('/proc/1/cgroup').readlines()[0],
).strip()

_, out, _ = cmd_output_b('docker', 'inspect', hostname)
_, out, _ = cmd_output_b('docker', 'inspect', container_id)

container, = json.loads(out)
for mount in container['Mounts']:
Expand Down

0 comments on commit 627209d

Please sign in to comment.