# Git clone private projects using ssh

**URL:** <https://forums.docker.com/t/git-clone-private-projects-using-ssh/136306>\
**Category:** General\
**Tags:** docker\
**Created:** [June 3, 2023, 3:31am UTC](https://forums.docker.com/t/git-clone-private-projects-using-ssh/136306 "2023-06-03T03:31:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tck199732](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@tck199732](https://forums.docker.com/u/tck199732)\
**Post date:** [June 3, 2023, 3:31am UTC](https://forums.docker.com/t/git-clone-private-projects-using-ssh/136306/1 "2023-06-03T03:31:09Z")

</div>

I am new to Docker and I have been trying to clone a private project using ssh without success. Here is my Dockerfile,

```auto
FROM continuumio/anaconda3
RUN apt-get update && apt-get install -y wget git openssh-client

ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh && chmod -R 700 /root/.ssh
RUN /bin/bash -c cat "${SSH_PRIVATE_KEY}" >> /root/.ssh/id_rsa 
RUN chmod 600 /root/.ssh/id_rsa && echo "StrictHostKeyChecking no" > /root/.ssh/config
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
# RUN eval "$(ssh-agent -s)" && ssh-add /root/.ssh/id_rsa

WORKDIR /workdir
RUN git clone git@github.com:USERNAME/REPO_NAME.git

```

To build, I use the command `docker build -t app --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)" .`However, it always return `Load key "/root/.ssh/id_rsa": invalid format`. To check if it is related github, I add the commented line `RUN eval "$(ssh-agent -s)" && ssh-add /root/.ssh/id_rsa` but it also return the same error.

I appreciate for your help.

---

<div class="post-metadata">

**Author:** ![alishah730](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/alishah730/32/40394_2.png) [@alishah730](https://forums.docker.com/u/alishah730)\
**Post date:** [June 3, 2023, 9:07am UTC](https://forums.docker.com/t/git-clone-private-projects-using-ssh/136306/2 "2023-06-03T09:07:34Z")

</div>

> [@tck199732](#):
>
> –build-arg SSH\_PRIVATE\_KEY=“$(cat ~/.ssh/id\_rsa)” .

Hello @tck199732 ,

please note cat may not be applicable to a variable.

please find updated docker file below.

```auto
FROM continuumio/anaconda3
ARG SSH_PRIVATE_KEY=""
RUN apt-get update && apt-get install -y wget git openssh-client
# ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh && chmod -R 700 /root/.ssh
RUN echo "${SSH_PRIVATE_KEY}" >> /root/.ssh/id_rsa
RUN echo ${SSH_PRIVATE_KEY}
RUN chmod 0400 /root/.ssh/id_rsa && echo "StrictHostKeyChecking no" > /root/.ssh/config
RUN cat /root/.ssh/id_rsa
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
# RUN eval "$(ssh-agent -s)" && ssh-add /root/.ssh/id_rsa

WORKDIR /workdir
RUN git clone git@github.com:USERNAME/REPO_NAME.git

RUN ls -l

```

I hope this helps.

thanks

---

<div class="post-metadata">

**Author:** ![meyay](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/meyay/32/34000_2.png) [@meyay](https://forums.docker.com/u/meyay)\
**Post date:** [June 3, 2023, 10:50am UTC](https://forums.docker.com/t/git-clone-private-projects-using-ssh/136306/3 "2023-06-03T10:50:59Z")

</div>

Do you really want your key to be persisted in the final image? Or is it supposed to be a multi-stage build, where the cloning and building takes place in a stage and the final result is copied into the final stage?

There is actually a build in way to mount ssh private keys as secrets into the build containers: [https://docs.docker.com/engine/reference/builder/#run---mounttypessh](https://docs.docker.com/engine/reference/builder/#run---mounttypessh)

Note: `# syntax=docker/dockerfile:1` in the first line is important, it needs to be present in the Dockerfile to make this special mounts work!

---

<div class="post-metadata">

**Author:** ![tck199732](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@tck199732](https://forums.docker.com/u/tck199732)\
**Post date:** [June 3, 2023, 11:52am UTC](https://forums.docker.com/t/git-clone-private-projects-using-ssh/136306/4 "2023-06-03T11:52:36Z")

</div>

Hi everyone,

@alishah730 I realized I made a mistake when writing ssh key into the target file.

```auto
# prints empty line to the file
RUN /bin/bash -c echo "${SSH_PRIVATE_KEY}" >> /root/.ssh/id_rsa
# works
RUN /bin/bash -c "echo '${SSH_PRIVATE_KEY}' >> /root/.ssh/id_rsa"

```

@meyay Thanks. I understand private key should not be exposed and this script is just a test. I was planning for a multi-stage build but it seems your suggestion is cleaner.

Sorry for making the stupid mistake. Thanks.
