Search code examples
gitdockerdocker-build

Why does docker build fail in a newly-initialized git repo?


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 pull request 1592 though whether your buildx is up to date with that is questionable since it was only merged to master three weeks back (as at the time this question was asked).

    And the first release containing that PR was 0.10.3, released on Feb 16, 2023 (roughly ten days ago, again as at the time of this question).

    Until it is up to date, I'd suggest you just ensure you always have a HEAD so this warning doesn't happen.


    Addressing your update about why docker and git are interacting, it's to provide useful information to the build environment should you wish to use it. See here for the git.go file, which shows the items extracted, such as labels, the 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.

    Issue 1290 was the one that brought this git.go file into existence, there's a far more detailed explanation there, including the text:

    Therefore, we'd like to propose that buildx starts to record the following pieces of provenance when being run with a context that has a .git directory:

    • Git commit SHA of current checked out HEAD.
    • Include flag to indicate state of local clone (dirty).
    • Dockerfile path.