Fix git error “fatal: detected dubious ownership in repository” in Docker


TLDR: The most reliable way for me was to set the correct safe.directory using environment variables in the appropriate Dockerfile like so:

ENV GIT_CONFIG_COUNT=1
ENV GIT_CONFIG_KEY_0=safe.directory
ENV GIT_CONFIG_VALUE_0=<path to directory>

Documentation on these environment variables and the config file is here.

What happened?

I’ve been idly working through setting up a local environment based on XWP’s VIP Site Template. When I went to run tests of the phpunit tests, I got this error:

The repository at “/var/www/html/wp-content” does not have the correct ownership and git refuses to use it:

fatal: detected dubious ownership in repository at ‘/var/www/html/wp-content’
To add an exception for this directory, call:

git config --global --add safe.directory /var/www/html/wp-content

I tried various ways and places to run the suggested command but the only thing that worked was going into the Docker shell and running the command in the appropriate place.

So I found I could use environment variables in the wordpress Dockerfile instead which has worked well.

Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting this comment, you are agreeing to the use of Akismet which helps reduce spam. You can view Akismet’s privacy policy here. Your email, website and name are also stored on this site.