Why does docker build fail/warn when run in a newly-initialized git repo?
$ mkdir dockergit
$ cd dockergit
$ git init
Initialized empty Git repository in /home/user/projects/foo/dockergit/.git/
$ cat << EOF > ./Dockerfile
> FROM alpine:3.7 as base
> EOF
$ docker build -t tmp:tmp .
[+] Building 0.2s (5/5) FINISHED
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 61B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.7 0.0s
=> [1/1] FROM docker.io/library/alpine:3.7 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:a3b9ee7f5e9bf7ad39ad1e9893be65001bd6367c7d10e2a682985d03e46458a9 0.0s
=> => naming to docker.io/library/tmp:tmp 0.0s
WARNING: buildx: failed to get git commit: fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$
$ git --version
git version 2.11.0
$ docker --version
Docker version 23.0.0, build e92dd87
Update: Addressing the comments that "HEAD doesn’t exist yet": what I don’t understand is why there’s any interaction between git and docker at all. I.e. why is docker build trying to/triggering anything related to git?
>Solution :
This particular bug was fixed in PR1592 though whether your buildx is up to date with that is questionable since it was only a few weeks ago.
Until it is up to date, I’d suggest you just ensure you always have a HEAD.
Addressing your update about why docker and git are interacting, it’s almost certainly to provide useful information to the build environment should you wish to use it. See here for an example of the items extracted, things like labels, commit point, whether state is dirty (changed files after commit point), and so on.
For example, we like to bake the commit point into the application and log it on startup, so we know exactly what software is running when a bug report rolls in.