Skip to content

Commit

Permalink
Merge pull request #1997 from pre-commit/true-dind
Browse files Browse the repository at this point in the history
ignore self-container when in docker-in-docker
  • Loading branch information
asottile committed Aug 3, 2021
2 parents a4444f1 + 5d1cac6 commit 6cfdabb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pre_commit/languages/docker.py
Expand Up @@ -8,6 +8,7 @@
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

Expand Down Expand Up @@ -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']:
Expand Down
8 changes: 8 additions & 0 deletions tests/languages/docker_test.py
Expand Up @@ -8,6 +8,7 @@
import pytest

from pre_commit.languages import docker
from pre_commit.util import CalledProcessError

DOCKER_CGROUP_EXAMPLE = b'''\
12:hugetlb:/docker/c33988ec7651ebc867cb24755eaf637a6734088bc7eef59d5799293a9e5450f7
Expand Down Expand Up @@ -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'

0 comments on commit 6cfdabb

Please sign in to comment.