title: Error deploying Docker Container to EC2 via GitLab CI/CD: load pubkey "/root/.ssh/id_rsa": invalid format
body: <p>I'm currently trying to deploy my NodeJS API by dockerising it and put it onto an EC2, however I've head a dead end with this error: <code>load pubkey &quot;/root/.ssh/id_rsa&quot;: invalid format</code></p>
  <p><strong>Things I've tried</strong></p>
  <ul>
  <li>Converting my Private RSA Key from PSK1 to PSK8</li>
  <li>Setting protected variable to false
  (and various combinations of the above)</li>
  <li>Using <code>apt-get install openssl-client</code> and converting the Private RSA Key from within the docker (but apparently I'm missing <code>apt-get</code> so I've also tried <code>apk</code> which also didn't work so... ????)</li>
  </ul>
  <p><strong>Questionable:</strong></p>
  <ul>
  <li>The error mentions <code>pubkey</code>, but all tutorials have pointed to using my <code>pem</code> key which is generated from my EC2 setup. So perhaps I'm missing a pubkey? But I can't find any material saying otherwise.</li>
  </ul>
  <p><strong>gitlab-ci.yml</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>    image: docker:19.03.12
      variables:
        DOCKER_TLS_CERTDIR: &quot;/certs&quot;
        DOCKER_DRIVER: overlay2
      services:
        - docker:19.03.0-dind
      before_script:
        - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
      stages:
        - build
        - deploy
      build:
        stage: build
        script:
          # - docker build --tag $CI_REGISTRY/$CI_PROJECT_PATH:latest . 
          # - docker push $CI_REGISTRY/$CI_PROJECT_PATH:latest
          - docker pull $CI_REGISTRY_IMAGE:latest || true
          - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY/$CI_PROJECT_PATH:latest . 
          - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
          - docker push $CI_REGISTRY/$CI_PROJECT_PATH:latest
      deploy:
        stage: deploy
        only:
          - master
        script:
          ###### Login to deployment server using SSH #####
          - mkdir -p ~/.ssh
          - echo &quot;$DEPLOY_SERVER_PRIVATE_KEY&quot; | tr -d '\r' &gt; ~/.ssh/id_rsa
          - chmod 700 ~/.ssh/id_rsa
          - '[[ -f ./dockerenv ]] &amp;&amp; echo -e &quot;Host *\n\tStrickHostKeyChecking no\n\n&quot; &gt; ~/.ssh/config'
          - 'which ssh-agent || ( apt-get update -y &amp;&amp; apt-get install openssh-client -y )'
          - eval &quot;$(ssh-agent -s)&quot;
          - ssh-add ~/.ssh/id_rsa
          - ssh-keyscan -H $DEPLOYMENT_SERVER_IP &gt;&gt; ~/.ssh/known_hosts  
          ###### Pull the registry to deployment server and recereate the docker service #####    
          - ssh $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER_IP docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY 
          - ssh $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER_IP docker pull $CI_REGISTRY/$CI_PROJECT_PATH:latest
          - ssh $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER_IP docker run $CI_REGISTRY/$CI_PROJECT_PATH:latest
  </code></pre>
  <p><strong>gitlab-runner config.toml</strong></p>
  <pre><code>    [[runners]]
        name = &quot;docker-runner&quot;
        url = &quot;https://gitlab.com/&quot;
        token = &quot;secretkey&quot;
        executor = &quot;docker&quot;
        [runners.custom_build_dir]
        [runners.cache]
          [runners.cache.s3]
          [runners.cache.gcs]
        [runners.docker]
          tls_verify = false
          image = &quot;docker:19.03.12&quot;
          privileged = true
          disable_entrypoint_overwrite = false
          oom_kill_disable = false
          disable_cache = false
          volumes = [&quot;/certs/client&quot;, &quot;/cache&quot;]
          shm_size = 0
  </code></pre>
  <p><strong>CI runner output</strong></p>
  <pre><code>    $ ssh $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER_IP docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
      load pubkey &quot;/root/.ssh/id_rsa&quot;: invalid format
      Login Succeeded
      $ ssh $DEPLOYMENT_SERVER_USERNAME@$DEPLOYMENT_SERVER_IP docker pull $CI_REGISTRY/$CI_PROJECT_PATH:latest
      load pubkey &quot;/root/.ssh/id_rsa&quot;: invalid format
  </code></pre>