title: Git clone not working inside Docker build despite SSH credentials present
body: <p>I'm attempting to clone a repository into an image from a machine where I'm successfully able to use SSH to clone the repository outside of the Docker build. However, every time I attempt to clone the repository I get denied with the following excuse:</p>
  
  <pre><code>Cloning into 'my-repo'...
  Permission denied (publickey).
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights and the repository exists.
  </code></pre>
  
  <p>Here is my Dockerfile:</p>
  
  <pre><code>FROM ubuntu as my-repo
  
  ARG GIT_SSH_KEY
  ARG GIT_SSH_PUBLIC_KEY
  ARG KNOWN_HOSTS
  
  RUN apt-get update
  RUN apt-get install -y git ssh
  
  # populate id_rsa files, populate known_hosts and config files, and manage permissions
  RUN mkdir ~/.ssh/ &amp;&amp; \
      chmod 700 ~/.ssh &amp;&amp; \
      touch ~/.ssh/id_rsa &amp;&amp; \
      touch ~/.ssh/id_rsa.pub &amp;&amp; \
      chmod 600 ~/.ssh/id_rsa &amp;&amp; \
      chmod 600 ~/.ssh/id_rsa.pub &amp;&amp; \
      touch ~/.ssh/known_hosts &amp;&amp; \
      touch ~/.ssh/config &amp;&amp; \
      chmod 600 ~/.ssh/known_hosts &amp;&amp; \
      chmod 600 ~/.ssh/config &amp;&amp; \
      echo "${GIT_SSH_KEY}" &gt; ~/.ssh/id_rsa &amp;&amp; \
      echo "${GIT_SSH_PUBLIC_KEY}" &gt; ~/.ssh/id_rsa.pub &amp;&amp; \
      echo "${KNOWN_HOSTS}" &gt; ~/.ssh/known_hosts &amp;&amp; \
      echo "Host bitbucket.example.com\n\tStrictHostKeyChecking no\n" &gt; ~/.ssh/config &amp;&amp; \
      git clone ssh://git@bitbucket.example.com/my-repo.git &amp;&amp; \
      rm ~/.ssh/*
  </code></pre>
  
  <p>I have verified that the Arguments passed in are making it as the same values as those fields on my local machine by debugging with echos. Furthermore, running <code>git clone ssh://git@bitbucket.example.com/my-repo.git</code> works fine on the machine.</p>
  
  <p>What am I missing that causes the permission to be denied? It states publickey as the reason but the <code>id_rsa.pub</code> is correctly populated.</p>
  
  <p>EDIT: here is the docker command
  <code>docker build --build-arg GIT_SSH_KEY="$GIT_SSH_KEY" --build-arg GIT_SSH_PUBLIC_KEY="$GIT_SSH_PUBLIC_KEY" --build-arg KNOWN_HOSTS="$KNOWN_HOSTS" -f myRepoDockerfile -t myRepo .</code></p>
  
  <p>I obtained all of the environment variables by running things such as
  <code>KNOWN_HOSTS='cat ~/.ssh/known_hosts'</code></p>