Using pre-commit in a docker-compose environment

Hello, I have recently been playing with pre-commit and have it set up for a project that doesn’t use docker containers. I am now looking at adding it to a project that does use docker containers with docker-compose and I am curious what the best practice is.

Currently, I develop this project on Windows using WSL2 and VS Code. I have a docker-compose file that I use for development which will allow me to run the runserver command and access it with a Windows browser. I use git in WSL2, not inside the container. I also use poetry inside the container to manage dependencies.

Now I want to add pre-commit. I can think of a few ways this could be done.

  1. Install poetry in WSL2, install pre-commit in the WSL2 poetry environment, run git commands in WSL2. This would duplicate the environment in both WSL2 and in the container.
  2. Install pre-commit inside the container using poetry, run git commands inside the container
  3. Install pre-commit in WSL2, run the pre-commit commands inside the container using the docker or docker_image hooks

Thanks in advance.

I’ve also recently run into this problem and am curious about best practices.

For me, the idea of switching to use git in the container feels a bit clunky, so I eliminated your option 2. I’m currently running git and pre-commit in a standalone python environment outside the container (your option 1). My pre-commit config is pretty basic (just running black and isort) so it doesn’t need access to the full project environment, just a small environment with those three tools. I think of it almost like a global environment on the host for working with git/python projects, and it can be shared by multiple docker projects with the same pre-commit config.

Again, not sure if that’s the best practice, but it’s where I’ve landed.