title: How to correctly pass ssh key file from Jenkins credentials variable into to docker build command?
body: <p>This question is a follow up to this question
  <a href="https://stackoverflow.com/questions/66193016/how-to-pass-jenkins-credentials-into-docker-build-command">How to pass jenkins credentials into docker build command?</a></p>
  <p>I am getting the ssh key file from jenkins credential store in my groovy pipeline and
  passing it into docker build command via --build-arg so that I can checkout and build artifacts from the private git repos from within my docker container</p>
  <p>credentials store id : cicd-user, which works for checking out my private works as expected from my groovy Jenkinsfile</p>
  <pre><code>checkout([$class: 'GitSCM',
              userRemoteConfigs: [[credentialsId: 'cicd-user', url:'ssh://git@bitbucket.myorg.co:7999/A/software.git']]
  </code></pre>
  <p>I access it and try to pass the same to docker build command:</p>
  <pre><code>  withCredentials([sshUserPrivateKey(credentialsId: 'cicd-user', keyFileVariable: 'FILE')]) { 
             sh &quot;cd ${WORKSPACE} &amp;&amp; docker build -t ${some-name} --build-arg USERNAME=cicd-user --build-arg  PRIV_KEY_FILE=\$FILE --network=host -f software/tools/jenkins/${some-name}/Dockerfile .&quot;
          }
  </code></pre>
  <p>in Dockerfile I do</p>
  <pre><code>RUN echo &quot;$PRIV_KEY_FILE&quot; &gt; /home/&quot;$USERNAME&quot;/.ssh/id_rsa &amp;&amp; \
   chmod 700 /home/&quot;$USERNAME&quot;/.ssh/id_rsa 
  </code></pre>
  <p>RUN echo &quot;Host bitbucket.myorg.co\n\tStrictHostKeyChecking no\n&quot; &gt;&gt; ~/.ssh/config</p>
  <p>But I am seeing the following issue</p>
  <blockquote>
  <p>&quot;Load key &quot;/home/cicd-user/.ssh/id_rsa&quot; :(invalid format)
  &quot;git@Bitbucket.mycomp.co:Permission denied( Public key)
  &quot;fatal: could not read from remote repository&quot;</p>
  </blockquote>
  <p>In the past I have passed the ssh priv key as --build-arg from outside by cat'ing like below</p>
  <p>--build-arg ssh_prv_key=&quot;$(cat ~/.ssh/id_rsa)&quot;</p>
  <p>Should I do something similar</p>
  <p>--build-arg PRIV_KEY_FILE=&quot;$(cat $FILE)&quot;</p>
  <p>Any idea on what might be going wrong or where I should be looking for debugging this correctly ?</p>