Repository navigation
add PRE_COMMIT_CONTAINER_RUNTIME to select docker or podman - #3780
Closed
robjackstewart wants to merge 1 commit into
Closed
robjackstewart wants to merge 1 commit into
robjackstewart wants to merge 1 commit into
Conversation
An opt-in, validated environment variable that selects the executable used by the docker and docker_image languages. Unset or empty keeps the current behaviour (docker). Values are trimmed and lowercased and must be docker or podman, anything else is a FatalError.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
PRE_COMMIT_CONTAINER_RUNTIMEto choosedockerorpodmanFirst of all, thank you for pre-commit. It's one of the few tools every team in our organisation has adopted without being told to: one config file, the same behaviour in every repo and on every OS, and it catches real problems (secrets, in our case, with a TruffleHog
docker_imagehook) before they leave a laptop.What this does
It adds an opt-in environment variable,
PRE_COMMIT_CONTAINER_RUNTIME, that selects the executable used by thedockeranddocker_imagelanguages.docker, exactly as today. Nothing changes unless you ask for it.dockerorpodman: that executable is used. The value is trimmed and lowercased first, so" Podman "works.nerdctl, a path,podman --remote, ...) raises aFatalErrornaming the variable and the supported values. A typo is never silently ignored, and this can't be used to run an arbitrary executable._docker_exe()replaces the hard-coded'docker'in the four places that call the runtime:build,run,system info(rootless detection) andinspect.docker_imagepicks it up throughdocker_cmd(), so there's no change in that file.It follows the pattern
languages/conda.pyalready uses (PRE_COMMIT_USE_MAMBA/PRE_COMMIT_USE_MICROMAMBA), and the name and thedocker/podmanvalues follow .NET Aspire'sASPIRE_CONTAINER_RUNTIME, which solves the same problem the same way.Why
I know this has come up before, and that the usual answer is to symlink
podmantodocker. I read the earlier threads and tried to keep this within what you said in them:.pre-commit-config.yamland no CLI flag. It's an environment variable, likePRE_COMMIT_HOMEandPRE_COMMIT_USE_MAMBA.dockersystem infooutput.:Zon some mounts is an upstream Podman problemWhy a symlink doesn't work for us. Some of our teams use Docker Swarm and
docker stack(docker swarm init/join,docker stack deploy,docker service,docker secret,docker config). Podman doesn't implement Swarm or stacks, so on those machinesdockerhas to be real Docker. Other teams don't need Docker Desktop and are standardising on Podman, and plenty of developers do both. A symlink is one machine-wide choice of whatdockermeans, so on a machine with both we'd have to pick betweendocker stack deployand pre-commit.Why an environment variable is better than a symlink or PATH shim:
binback at the front and quietly undo the shim). A bad value for the variable fails loudly with a clear message. A bad symlink quietly runs the wrong runtime.ln -son Unix but needs admin rights or Developer Mode on Windows, and adocker.cmdshim isn't a safe substitute because of how cmd.exe re-parses arguments.I appreciate that the use case is narrow. I still think the tool is easier to use for everyone with an explicit, validated variable than with symlinks.
Testing
I added 15 tests to
tests/languages/docker_test.py, written in the same style as the conda ones (envcontext,mock.patch.object):docker,podman,PODMANand" Podman "FatalErrorfornerdctl,/usr/bin/podman,podman --remoteanddocker;podman, including that the message names the variable and the supported valuesdocker_cmd()and the rootless check use the selected runtimeAll 15 pass on Windows 11. On the same machine, 5 existing
test_docker_user_non_rootlesstests fail with and without this change, becauseos.getuiddoesn't exist on Windows. The repo's pre-commit hooks pass on the changed files, apart from mypy, which only reports Windows-only errors on lines I didn't touch (getuid,getgid,sched_getaffinity).What I haven't done: I haven't yet run this against a live Podman engine, and I haven't run the suite on Linux or macOS, so I'm relying on CI for those.
Docs
The variables are documented in the separate
pre-commit.comrepo. If you're happy with the approach I'll send a PR there to add this one next toPRE_COMMIT_USE_MAMBA.If this isn't something you want in pre-commit, I understand, and thank you for taking the time to look at it anyway.