Merge pull request #1997 from pre-commit/true-dind

ignore self-container when in docker-in-docker
This commit is contained in:
Anthony Sottile
2021-08-03 16:41:21 -04:00
committed by GitHub
2 changed files with 14 additions and 1 deletions

View File

@@ -8,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
@@ -42,7 +43,11 @@ def _get_docker_path(path: str) -> str:
container_id = _get_container_id()
_, out, _ = cmd_output_b('docker', 'inspect', 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']:

View File

@@ -8,6 +8,7 @@ from unittest import mock
import pytest
from pre_commit.languages import docker
from pre_commit.util import CalledProcessError
DOCKER_CGROUP_EXAMPLE = b'''\
12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
@@ -171,3 +172,10 @@ def test_get_docker_path_in_docker_windows(in_docker):
path = r'c:\folder\test\something'
expected = r'c:\users\user\test\something'
assert docker._get_docker_path(path) == expected
def test_get_docker_path_in_docker_docker_in_docker(in_docker):
# won't be able to discover "self" container in true docker-in-docker
err = CalledProcessError(1, (), 0, b'', b'')
with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
assert docker._get_docker_path('/project') == '/project'