title: How to pass local machine's SSH key to docker container?
body: <p>I'm trying to build a docker image from Dockerfile and one of the steps that need to be taken is installing a dependency that is only available via private Gitlab repository. This means the container will need to have access to SSH keys to do the clone. I know this isn't the most secure approach, however this is only going to be an intermediate container that is going to be removed once all of the components necessary to run the app are in place. </p>
  
  <p>The problem is, that I cannot, whatever I try, get ssh agent inside docker to establish the connection. I get:</p>
  
  <pre><code>npm ERR! Host key verification failed.
  npm ERR! fatal: Could not read from remote repository.
  npm ERR! 
  npm ERR! Please make sure you have the correct access rights
  npm ERR! and the repository exists.
  </code></pre>
  
  <p>The same thing happens if I try to simply clone the repository without running <code>npm install</code>. Here is the Dockerfile I use:</p>
  
  <pre><code>FROM risingstack/alpine:3.4-v6.9.4-4.2.0
  
  
  RUN apk update
  
  RUN apk add openssh
  
  ARG SSH_KEY
  
  # Authorize SSH Host
  RUN mkdir -p /root/.ssh &amp;&amp; \
      chmod 700 /root/.ssh &amp;&amp; \
      ssh-keyscan github.com &gt; /root/.ssh/known_hosts
  
  # Add the keys and set permissions
  RUN echo "$SSH_KEY" &gt; /root/.ssh/id_rsa &amp;&amp; \
      chmod 700 /root/.ssh/id_rsa &amp;&amp; \
  
  
  RUN eval "$(ssh-agent -s)" &amp;&amp; ssh-add /root/.ssh/id_rsa &amp;&amp; ssh -o StrictHostKeyChecking=no git@github.com || true &amp;&amp; npm install
  </code></pre>
  
  <p>and the command (I pass the private key as build argument): </p>
  
  <pre><code>docker build -t test  --build-arg SSH_KEY="$(cat ~/.ssh/id_rsa)" .
  </code></pre>