Sharing .gradle between host and containers

I have a development paradigm where each of my projects is a docker-compose setup. It’s quite handy to switch from one project to another, to setup a development environment on a new machine, to isolate databases and services, etc.

So far so good. But when a project involves an embedded service which is launched by a ./gradlew run command inside a docker container, calling ./gradlew on the host (for instance to build some resource that is supposed to trigger some autoreload in the container) always results in a timeout (the gradle client somehow sees the gradle dameon running in the container, but of course fails to connect to it).

The reason for this is probably that the host and the container are sharing the same .gradle directory at the root of the project, but I’d like to understand the real cause, to try to find a workaround.