'Dockerfile writing ssh private key environment variable to /root/.ssh/id_rsa

I have environment variable stored in aws parameter store. When aws codebuild run i want to be able to copy or write the environment variable to /root/.ssh/id_rsa so that i can be able to clone the repo. When the image is build, this error is thrown: Load key "/root/.ssh/id_rsa": invalid format.

FROM php:8.0-fpm
ARG SSHPRIVATE_KEY=$GIT_SSHPRIVATE_KEY
ARG SSHPUBLIC_KEY=$GIT_SSHPUBLIC_KEY

RUN mkdir /root/.ssh/
RUN echo "${SSHPRIVATE_KEY}" > /root/.ssh/id_rsa
RUN echo "${SSHPUBLIC_KEY}" > /root/.ssh/id_rsa.pub


Solution 1:[1]

I ended up, setting things up from the buildspec file. I access the source artifact then i write the parameter store value to the id_rsa file.

      - cd $CODEBUILD_SRC_DIR_MySourceArtifacts
      - echo "$BITBUCKET_SSH_KEY" >> id_rsa

Then on the Dockerfile i copy id_rsa to the appropriate docker container folder.

COPY $CODEBUILD_SRC_DIR_MySourceArtifacts/id_rsa /root/.ssh

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user11766958