'Echo SSH key into the container messes docker build output

I need to use SSH keys inside a container during build stage and I do that with

RUN echo "${SSH_KEY}" > /root/.ssh/id_rsa

Where SSH_KEY is build arg. The problem is, once this command is done, the output is messed up:

 => [internal] load build definition from Dockerfile          0.0s
[+] Building 733.0s (21/22)
 => [internal] load build definition from Dockerfile          0.0s
[+] Building 733.2s (21/22)
 => [internal] load build definition from Dockerfile          0.0s
[+] Building 733.3s (21/22)
 => [internal] load build definition from Dockerfile          0.0s
[+] Building 733.5s (21/22)
 => [internal] load build definition from Dockerfile          0.0s
[+] Building 733.6s (21/22)
 => [internal] load build definition from Dockerfile          0.0s
[+] Building 733.6s (22/22) FINISHED

Above is printed repeatedly until the build is done. Is there anything I can do about that?

Otherwise, the container building works fine.



Solution 1:[1]

As commenters suggested, using --mount=type=ssh flag for RUN git clone lines works a lot better.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 q30a