`Load key /root/.ssh/id_rsa : invalid format`

Hi,

I am facing Load key "/root/.ssh/id_rsa": invalid format issue while building docker image.

Sending build context to Docker daemon     18MB
Step 1/17 : FROM  node:8-alpine as intermediate
 ---> 2b8fcdc6230a
Step 2/17 : LABEL stage=intermediate
 ---> Running in 464fae525503
Removing intermediate container 464fae525503
 ---> db81fd3e26db
Step 3/17 : ARG SSH_PVT_KEY
 ---> Running in a7fddc26f1d5
Removing intermediate container a7fddc26f1d5
 ---> 179040e57166
Step 4/17 : ARG GIT_BRANCH
 ---> Running in e5c5e6aa099d
Removing intermediate container e5c5e6aa099d
 ---> 148801637228
Step 5/17 : ARG CLIENT_GIT
 ---> Running in 4bcf97747ff4
Removing intermediate container 4bcf97747ff4
 ---> 0a2dc670fdce
Step 6/17 : ARG GIT_REPO
 ---> Running in e559db489f9a
Removing intermediate container e559db489f9a
 ---> 97d664c24c9a
Step 7/17 : ARG REACT_APP_BASE_API_URL
 ---> Running in 84add1968f13
Removing intermediate container 84add1968f13
 ---> 3c715cd88aff
Step 8/17 : ARG REACT_APP_client_id
 ---> Running in 6357a269f1ea
Removing intermediate container 6357a269f1ea
 ---> 43bb2d50ab1e
Step 9/17 : ARG REACT_APP_client_secret
 ---> Running in cd14e6838d91
Removing intermediate container cd14e6838d91
 ---> 7dfd0c2cba36
Step 10/17 : ARG REACT_APP_T_REDOC_URL
 ---> Running in 1d96227d7a8c
Removing intermediate container 1d96227d7a8c
 ---> fae78090c164
Step 11/17 : ARG REACT_APP_ENABLE_onelogin_LOGIN
 ---> Running in 425d44db9fdd
Removing intermediate container 425d44db9fdd
 ---> c5ddb239ec13
Step 12/17 : ENV GIT_URL=git@github.com:${CLIENT_GIT}/${GIT_REPO}.git
 ---> Running in 06f752250cbb
Removing intermediate container 06f752250cbb
 ---> 3c56e9c94d45
Step 13/17 : WORKDIR /tmp
 ---> Running in a5e1c2c8fb0f
FROM  node:8-alpine as intermediate
LABEL stage=intermediate

ARG SSH_PVT_KEY
ARG GIT_BRANCH
ARG CLIENT_GIT
ARG GIT_REPO
ARG REACT_APP_BASE_API_URL
ARG REACT_APP_client_id
ARG REACT_APP_client_secret
ARG REACT_APP_T_REDOC_URL
ARG REACT_APP_ENABLE_onelogin_LOGIN

ENV GIT_URL=git@github.com:${CLIENT_GIT}/${GIT_REPO}.git

WORKDIR /tmp
RUN apk add --update \
    git \
    openssh-client \
    && mkdir -p /root/.ssh/ \
    && chmod 700 /root/.ssh \
    && echo "${SSH_PVT_KEY}" > /root/.ssh/id_rsa \
    && chmod -R 600 /root/.ssh/ \
    && echo "" > /root/.ssh/config \
    && echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config \
    && git clone "${GIT_URL}" \
    && cd "${GIT_REPO}" \
    && git checkout "${GIT_BRANCH}" \
    && git pull origin "${GIT_BRANCH}" \
    && rm -rf .git \
    && cp .env.prod .env \
    && sed -ir "s|^REACT_APP_BASE_API_URL = .*|REACT_APP_BASE_API_URL = ${REACT_APP_BASE_API_URL}|g" .env \
    && sed -ir "s|^REACT_APP_client_id = .*|REACT_APP_client_id = ${REACT_APP_client_id}|g" .env \
    && sed -ir "s|^REACT_APP_client_secret = .*|REACT_APP_client_secret = ${REACT_APP_client_secret}|g" .env \
    && sed -ir "s|^REACT_APP_T_REDOC_URL = .*|REACT_APP_T_REDOC_URL = ${REACT_APP_T_REDOC_URL}|g" .env \
    && sed -ir "s|^REACT_APP_ENABLE_onelogin_LOGIN = .*|REACT_APP_ENABLE_onelogin_LOGIN = ${REACT_APP_ENABLE_onelogin_LOGIN}|g" .env \
    && npm install \
    && npm run-script build \
    && cp .htaccess build

FROM nginx:1.17.2-alpine
COPY --from=intermediate /tmp/devtestportal-ui
/build/ /var/www/html/frontend/build

RUN rm /etc/nginx/conf.d/default.conf \
"Dockerfile" 45L, 1640C                                                                                                                                         22,5          Top
FROM  node:8-alpine as intermediate
Removing intermediate container a5e1c2c8fb0f
 ---> 31048a3d5b5b
Step 14/17 : RUN apk add --update     git     openssh-client     && mkdir -p /root/.ssh/     && chmod 700 /root/.ssh     && echo "${SSH_PVT_KEY}" > /root/.ssh/id_rsa     && chmod -R 600 /root/.ssh/     && echo "" > /root/.ssh/config     && echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config     && git clone "${GIT_URL}"     && cd "${GIT_REPO}"     && git checkout "${GIT_BRANCH}"     && git pull origin "${GIT_BRANCH}"     && rm -rf .git     && cp .env.prod .env     && sed -ir "s|^REACT_APP_BASE_API_URL = .*|REACT_APP_BASE_API_URL = ${REACT_APP_BASE_API_URL}|g" .env     && sed -ir "s|^REACT_APP_client_id = .*|REACT_APP_client_id = ${REACT_APP_client_id}|g" .env     && sed -ir "s|^REACT_APP_client_secret = .*|REACT_APP_client_secret = ${REACT_APP_client_secret}|g" .env     && sed -ir "s|^REACT_APP_T_REDOC_URL = .*|REACT_APP_T_REDOC_URL = ${REACT_APP_T_REDOC_URL}|g" .env     && sed -ir "s|^REACT_APP_ENABLE_onelogin_LOGIN = .*|REACT_APP_ENABLE_onelogin_LOGIN = ${REACT_APP_ENABLE_onelogin_LOGIN}|g" .env     && npm install     && npm run-script build     && cp .htaccess build
 ---> Running in 9c60854382ef
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/11) Installing ca-certificates (20191127-r2)
(2/11) Installing nghttp2-libs (1.40.0-r1)
(3/11) Installing libcurl (7.67.0-r5)
(4/11) Installing expat (2.2.9-r1)
(5/11) Installing pcre2 (10.34-r1)
(6/11) Installing git (2.24.4-r0)
(7/11) Installing openssh-keygen (8.1_p1-r0)
(8/11) Installing ncurses-terminfo-base (6.1_p20200118-r4)
(9/11) Installing ncurses-libs (6.1_p20200118-r4)
(10/11) Installing libedit (20191211.3.1-r0)
(11/11) Installing openssh-client (8.1_p1-r0)
Executing busybox-1.31.1-r8.trigger
Executing ca-certificates-20191127-r2.trigger
OK: 28 MiB in 27 packages
Cloning into 'devtestportal-ui'...
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Load key "/root/.ssh/id_rsa": invalid format
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
The command '/bin/sh -c apk add --update     git     openssh-client     && mkdir -p /root/.ssh/     && chmod 700 /root/.ssh     && echo "${SSH_PVT_KEY}" > /root/.ssh/id_rsa     && chmod -R 600 /root/.ssh/     && echo "" > /root/.ssh/config     && echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config     && git clone "${GIT_URL}"     && cd "${GIT_REPO}"     && git checkout "${GIT_BRANCH}"     && git pull origin "${GIT_BRANCH}"     && rm -rf .git     && cp .env.prod .env     && sed -ir "s|^REACT_APP_BASE_API_URL = .*|REACT_APP_BASE_API_URL = ${REACT_APP_BASE_API_URL}|g" .env     && sed -ir "s|^REACT_APP_client_id = .*|REACT_APP_client_id = ${REACT_APP_client_id}|g" .env     && sed -ir "s|^REACT_APP_client_secret = .*|REACT_APP_client_secret = ${REACT_APP_client_secret}|g" .env     && sed -ir "s|^REACT_APP_T_REDOC_URL = .*|REACT_APP_T_REDOC_URL = ${REACT_APP_T_REDOC_URL}|g" .env     && sed -ir "s|^REACT_APP_ENABLE_onelogin_LOGIN = .*|REACT_APP_ENABLE_onelogin_LOGIN = ${REACT_APP_ENABLE_onelogin_LOGIN}|g" .env     && npm install     && npm run-script build     && cp .htaccess build' returned a non-zero code: 128
#
cat Dockerfile
FROM  node:8-alpine as intermediate
LABEL stage=intermediate

ARG SSH_PVT_KEY
ARG GIT_BRANCH
ARG CLIENT_GIT
ARG GIT_REPO
ARG REACT_APP_BASE_API_URL
ARG REACT_APP_client_id
ARG REACT_APP_client_secret
ARG REACT_APP_T_REDOC_URL
ARG REACT_APP_ENABLE_onelogin_LOGIN

ENV GIT_URL=git@github.com:${CLIENT_GIT}/${GIT_REPO}.git

WORKDIR /tmp
RUN apk add --update \
    git \
    openssh-client \
    && mkdir -p /root/.ssh/ \
    && chmod 700 /root/.ssh \
    && echo "${SSH_PVT_KEY}" > /root/.ssh/id_rsa \
    && chmod -R 600 /root/.ssh/ \
    && echo "" > /root/.ssh/config \
    && echo -e "Host *\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config \
    && git clone "${GIT_URL}" \
    && cd "${GIT_REPO}" \
    && git checkout "${GIT_BRANCH}" \
    && git pull origin "${GIT_BRANCH}" \
    && rm -rf .git \
    && cp .env.prod .env \
    && sed -ir "s|^REACT_APP_BASE_API_URL = .*|REACT_APP_BASE_API_URL = ${REACT_APP_BASE_API_URL}|g" .env \
    && sed -ir "s|^REACT_APP_client_id = .*|REACT_APP_client_id = ${REACT_APP_client_id}|g" .env \
    && sed -ir "s|^REACT_APP_client_secret = .*|REACT_APP_client_secret = ${REACT_APP_client_secret}|g" .env \
    && sed -ir "s|^REACT_APP_T_REDOC_URL = .*|REACT_APP_T_REDOC_URL = ${REACT_APP_T_REDOC_URL}|g" .env \
    && sed -ir "s|^REACT_APP_ENABLE_onelogin_LOGIN = .*|REACT_APP_ENABLE_onelogin_LOGIN = ${REACT_APP_ENABLE_onelogin_LOGIN}|g" .env \
    && npm install \
    && npm run-script build \
    && cp .htaccess build

FROM nginx:1.17.2-alpine
COPY --from=intermediate /tmp/devtestportal-ui
/build/ /var/www/html/frontend/build

RUN rm /etc/nginx/conf.d/default.conf \
    && chown -R nginx:nginx /var/www/html
Any clue? Thanks in Advance.

Best Regards,

Kaushal

You can use this utility GitHub - dockito/vault: 🔐 Secret store to be used on Docker image building. It works for me.