title: How can you automatically run and remove a container in docker compose
body: <p>I'm looking to forwarding my ssh-agent and found this
  <a href="https://github.com/nardeas/ssh-agent" rel="nofollow noreferrer">https://github.com/nardeas/ssh-agent</a></p>
  
  <p>and the steps are the following</p>
  
  <p><strong>0. Build</strong>
  Navigate to the project directory and launch the following command to build the image:</p>
  
  <pre><code>docker build -t docker-ssh-agent:latest -f Dockerfile .
  </code></pre>
  
  <p><strong>1. Run a long-lived container</strong></p>
  
  <pre><code>docker run -d --name=ssh-agent docker-ssh-agent:latest
  </code></pre>
  
  <p><strong>2. Add your ssh keys</strong>
  Run a temporary container with volume mounted from host that includes your SSH keys. SSH key id_rsa will be added to ssh-agent (you can replace id_rsa with your key name):</p>
  
  <pre><code>docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh -it docker-ssh-agent:latest ssh-add /root/.ssh/id_rsa
  </code></pre>
  
  <p><em>The ssh-agent container is now ready to use.</em></p>
  
  <p><strong>3. Add ssh-agent socket to other container:</strong>
  <em>If you're using docker-compose this is how you forward the socket to a container:</em></p>
  
  <pre><code>volumes_from:
    - ssh-agent
  environment:
    - SSH_AUTH_SOCK=/.ssh-agent/socket
  </code></pre>
  
  <p>in a compose file, I add step 1 to it like so:</p>
  
  <pre><code>services:
    ssh_agent:
      image: nardeas/ssh-agent
  </code></pre>
  
  <p>However I do not what's the equivalent syntax in <strong>compose file</strong> for step 2</p>
  
  <pre><code>docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh -it docker-ssh-agent:latest ssh-add /root/.ssh/id_rsa
  </code></pre>