This is a proof-of-concept of providing a pre-commit hook for flake8 via docker. It is suggested to instead use flake8 as provided by pre-commit-hooks as you'll have slightly better performance and more control over the python executable.
pre-commit by convention mounts the user's code at /src inside the
container. The executable is fed relative path filename arguments. The hook
may make changes to the files as the source is mounted read-write and the
executables are run as the user.
A few key lines in the Dockerfile make this work:
RUN virtualenv /venv -ppython3 && /venv/bin/pip install flake8
ENV PATH=/venv/bin:$PATHThis creates a virtual environment inside the docker image and puts the
virtualenv on the PATH so executables (such as flake8) can be run.
Lastly, the metadata in hooks.yaml hooks this up:
entry: flake8
language: dockerHere flake8 is the executable inside the container and we tell pre-commit
that the language is docker.
Well, you probably wouldn't (and the hook has been labeled as such) but if you
really wanted to you could add this to your .pre-commit-config.yaml:
- repo: https://github.com/pre-commit/pre-commit-docker-flake8
rev: '' # Fill this in with a current revision
hooks:
- id: flake8-docker-not-for-productionYou'll also need to use at least version 0.10.0 of pre-commit.