Menachem Hornbacher
Menachem Hornbacher

Reputation: 2166

Docker unable to copy .git folder

When trying to generate a file called head with the current commit hash during a docker build (for internal .NET service versioning) it seems that docker is unable to pull the .git folder into the image at all.

Given the following DockerFile

FROM alpine/git AS version
WORKDIR /src
COPY .git/ ./.git/
RUN git rev-parse HEAD > head

This happens:

 => ERROR [version 2/4] COPY .git/ ./.git/                                        0.0s
------
 > [version 2/4] COPY .git/ ./.git/:
------
failed to compute cache key: "/.git" not found: not found

What is perhaps more interesting is that when using COPY . . it fails like so:

 => ERROR [version 4/4] RUN git rev-parse HEAD > head                             1.7s
------
 > [version 4/4] RUN git rev-parse HEAD > head:
#36 1.613 fatal: not a git repository (or any of the parent directories): .git
------
executor failed running [/bin/sh -c git rev-parse HEAD > head]: exit code: 128

The git folder is at the same root as the Dockerfile as ls -Force (windows powershell version of ls -a) the following result is returned (a few folders redacted for privacy):

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         11/4/2020   2:45 PM                .ci
d--h--         3/17/2021   3:06 PM                .git
d-----          2/3/2021   6:12 PM                .github
d-----          9/8/2020   7:22 PM                .idea
d-----         1/20/2021   1:50 PM                .run
d--h--         9/29/2020   6:53 PM                .vs
d-----         3/17/2021  10:55 AM                build
d-----         11/4/2020   2:45 PM                lib
d-----          9/7/2020  11:12 AM                src
d-----         11/4/2020   2:45 PM                tests
-a----         3/15/2021   4:19 PM            340 .dockerignore
-a----          2/3/2021   6:12 PM            186 .editorconfig
-a----          2/3/2021   6:12 PM            580 .gitignore
-a----         3/17/2021   4:07 PM           1611 Dockerfile

Unhiding the .git does not change this behavior.

https://stackoverflow.com/a/54150671/1890717 is a related answer that does not seem to be working here. At least on Windows 10

Upvotes: 3

Views: 2698

Answers (1)

1615903
1615903

Reputation: 34733

Check if .git is included in your .dockerignore file and if so, remove it.

Upvotes: 8

Related Questions