SSH works with RUN but not with ADD

Hello everyone,

I want my image to clone a Github repository using SSH on build.

As suggested in the documentation, the build command I run is: sudo DOCKER_BUILDKIT=1 docker build --ssh default=$SSH_AUTH_SOCK .

To fulfill my requirement, I tried two approaches so far:

  1. With RUN --mount=type=ssh (Works fine, clones the repository successfully)
    Source: RUN --mount=type=ssh
# syntax=docker/dockerfile:1
FROM alpine
RUN apk update && \
		apk add --no-cache openssh-client && \
		apk add --no-cache git
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh \
  git clone git@github.com:mozancetin/c-projects.git
  1. With ADD <src>. (Does not work, cannot clone the repository. This approach only works when the repositories are requested with HTTPS, which is not possible for private repositories.)
    Source: Adding a private git repository
# syntax=docker/dockerfile:1
FROM alpine
ADD git@github.com:mozancetin/c-projects.git /

(This approach cannot authenticate to Github. I tried ssh -vvv git@github.com, that’s how I know.)

The reason I want ADD to work is that ADD automatically checks if there are differences between the cached one and the current one. That is an important feature I want to utilize.

So, what can be the problem? Do I misunderstand the documentation? Isn’t this the way I should expect ADD to work with SSH?