Fix docker-in-docker detection for cgroups v2 by br-rhrbacek · Pull Request #3535 · pre-commit/pre-commit
Repository navigation
Fix docker-in-docker detection for cgroups v2 - #3535
Merged
asottile merged 1 commit into pre-commit:main from br-rhrbacek:fix-cgroups Nov 8, 2025
Merged
Fix docker-in-docker detection for cgroups v2 #3535 asottile merged 1 commit into pre-commit:main from br-rhrbacek:fix-cgroups
Conversation
br-rhrbacek commented Sep 5, 2025
Copy link Copy Markdown
Contributor
Inspired by a similar issue in Jenkins, I propose to fix the broken container detection by using /proc/1/mountinfo.
br-rhrbacek mentioned this pull request Sep 5, 2025 Pre-commit unable to determine if it is in a container from /proc/1/cgroup for Docker-in-Docker setups #3360 ClosedJasonAlt commented Sep 5, 2025
Copy link Copy Markdown
|
This seems to work for me. I believe the change to the contents of |
View reviewed changes
Comment thread tests/languages/docker_test.py
Outdated
Comment on lines 136 to 138
| def test_in_docker_docker_in_file(): | ||
| with _mock_open( DOCKER_CGROUP_EXAMPLE ): | ||
| with _mock_open( DOCKER_MOUNTINFO_EXAMPLE ): | ||
| assert docker._is_in_docker() is True |
View reviewed changes
Comment thread pre_commit/languages/docker.py
Outdated
Comment on lines +32 to +42
| with open('/proc/1/mountinfo', 'rb') as f: | ||
| hostname_mount = re.compile( | ||
| r""" | ||
| /containers | ||
| (/overlay-containers)? | ||
| /([a-z0-9]{64}) | ||
| (/userdata)? | ||
| /hostname | ||
| """, | ||
| re.VERBOSE, | ||
| ) |
Outdated
| raise RuntimeError('Failed to find the container ID in /proc/1/cgroup.') | ||
| m = hostname_mount.search(line.decode()) | ||
| if m: | ||
| return m.group(2) |
Further adjustments to WSL detection
84ff52a
It seems that using /proc/1/cgroup is now broken to detect docker. Other projects have been using /proc/1/mountinfo instead (see pre-commit/pre-commit#3535)bidorffOL mentioned this pull request Oct 13, 2025 Further adjustments to WSL detection hashicorp/cap#169 Open 1 of 3 tasks
Fix docker-in-docker detection for cgroups v2
f80801d
asottile
force-pushed
the
fix-cgroups
branch
from
0d6d802 to
f80801d
Compare
November 8, 2025 20:37 asottile changed the title Fix pre-commit unable to determine if it is in a container for Docker-in-Docker setups (pre-commit#3360) Fix docker-in-docker detection for cgroups v2 Nov 8, 2025 asottile enabled auto-merge November 8, 2025 20:39 asottile merged commit
cb63a5c
into
pre-commit : main
Nov 8, 2025
tmeijn
pushed a commit
to tmeijn/dotfiles
that referenced
this pull request
Nov 14, 2025
build(deps): update pre-commit to v4.4.0
9e95828
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [pre-commit](https://github.com/pre-commit/pre-commit) | minor | `4.3.0` -> `4.4.0` | MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot). **Proposed changes to behavior should be submitted there as MRs.** --- ### Release Notes <details> <summary>pre-commit/pre-commit (pre-commit)</summary> ### [`v4.4.0`](https://github.com/pre-commit/pre-commit/blob/HEAD/CHANGELOG.md#440---2025-11-08) [Compare Source](pre-commit/pre-commit@v4.3.0...v4.4.0) \================== ##### Features - Add `--fail-fast` option to `pre-commit run`. - [#​3528](pre-commit/pre-commit#3528) MR by [@​JulianMaurin](https://github.com/JulianMaurin). - Upgrade `ruby-build` / `rbenv`. - [#​3566](pre-commit/pre-commit#3566) MR by [@​asottile](https://github.com/asottile). - [#​3565](pre-commit/pre-commit#3565) issue by [@​MRigal](https://github.com/MRigal). - Add `language: unsupported` / `language: unsupported_script` as aliases for `language: system` / `language: script` (which will eventually be deprecated). - [#​3577](pre-commit/pre-commit#3577) MR by [@​asottile](https://github.com/asottile). - Add support docker-in-docker detection for cgroups v2. - [#​3535](pre-commit/pre-commit#3535) MR by [@​br-rhrbacek](https://github.com/br-rhrbacek). - [#​3360](pre-commit/pre-commit#3360) issue by [@​JasonAlt](https://github.com/JasonAlt). ##### Fixes - Handle when docker gives `SecurityOptions: null`. - [#​3537](pre-commit/pre-commit#3537) MR by [@​asottile](https://github.com/asottile). - [#​3514](pre-commit/pre-commit#3514) issue by [@​jenstroeger](https://github.com/jenstroeger). - Fix error context for invalid `stages` in `.pre-commit-config.yaml`. - [#​3576](pre-commit/pre-commit#3576) MR by [@​asottile](https://github.com/asottile). </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNzMuMSIsInVwZGF0ZWRJblZlciI6IjQxLjE3My4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment