Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pre_commit/languages/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import os
import re
import shutil
from collections.abc import Sequence

from pre_commit import lang_base
Expand All @@ -31,6 +32,19 @@
)


@functools.lru_cache(maxsize=1)
def _get_container_cmd() -> str:
if shutil.which('docker') is not None:
return 'docker'
elif shutil.which('podman') is not None:
return 'podman'
else:
raise SystemExit(
'docker (or podman) is required for the docker language.\n'
'Install docker or podman to continue.',
)


def _get_container_id() -> str | None:
with contextlib.suppress(FileNotFoundError):
with open('/proc/1/mountinfo', 'rb') as f:
Expand All @@ -48,7 +62,7 @@ def _get_docker_path(path: str) -> str:
return path

try:
_, out, _ = cmd_output_b('docker', 'inspect', container_id)
_, out, _ = cmd_output_b(_get_container_cmd(), 'inspect', container_id)
except CalledProcessError:
# self-container was not visible from here (perhaps docker-in-docker)
return path
Expand Down Expand Up @@ -81,7 +95,7 @@ def build_docker_image(
pull: bool,
) -> None: # pragma: win32 no cover
cmd: tuple[str, ...] = (
'docker', 'build',
_get_container_cmd(), 'build',
'--tag', docker_tag(prefix),
'--label', PRE_COMMIT_LABEL,
)
Expand Down Expand Up @@ -109,7 +123,7 @@ def install_environment(
@functools.lru_cache(maxsize=1)
def _is_rootless() -> bool: # pragma: win32 no cover
retcode, out, _ = cmd_output_b(
'docker', 'system', 'info', '--format', '{{ json . }}',
_get_container_cmd(), 'system', 'info', '--format', 'json',
)
if retcode != 0:
return False
Expand Down Expand Up @@ -144,7 +158,7 @@ def get_docker_tty(*, color: bool) -> tuple[str, ...]: # pragma: win32 no cover

def docker_cmd(*, color: bool) -> tuple[str, ...]: # pragma: win32 no cover
return (
'docker', 'run',
_get_container_cmd(), 'run',
'--rm',
*get_docker_tty(color=color),
*get_docker_user(),
Expand Down
Loading