title: ssh-add in a docker container
body: <p>I've mounted the <code>~/.ssh</code> directory into my development docker container from the host machine and trying to add the deployment key to the ssh agent in the entrypoint like this:</p>
  <pre><code>#!/bin/bash
  set -e
  
  #add the deployment key
  if [[ ! -z ${DEPLOYMENT_SSH_KEY} ]] ; then
      echo &quot;Adding deployment ssh key ${DEPLOYMENT_SSH_KEY} to ssh agent&quot;
      eval $(ssh-agent -s) &amp;&amp; ssh-add /.ssh/${DEPLOYMENT_SSH_KEY}
  else
      echo &quot;Deployment ssh key is not defined&quot;
  fi
  
  echo &quot;STARTING PHP FPM, CMD $@&quot;
  
  docker-php-entrypoint &quot;$@&quot;
  </code></pre>
  <p>The key gets normally added according to the logs:</p>
  <blockquote>
  <p>Agent pid 8</p>
  <p>Adding deployment ssh key id_rsa to ssh agent</p>
  <p>Identity added: /.ssh/id_rsa (rsa w/o comment)</p>
  </blockquote>
  <p>However, it actually ends up with no keys added to the agent and I can't clone my repos from inside the container. The command <code>ssh-add -l</code> executed in the container says</p>
  <blockquote>
  <p>Could not open a connection to your authentication agent.</p>
  </blockquote>
  <p>Why's that? I heard of the SSH agent forwarding as an alternative but I'm wondering why the method I've tried doesn't work?</p>