I am trying to clone a repository inside a docker image using a docker file. I am running in Ubuntu 18.04 with docker version 20.10.17. I know docker files are automatic and do not allow user input. Therefore I need to clone via ssh. I started with:
ssh-keygen
ssh-add -k ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub | xclip -sel clip
I then pasted the key into bitbucket as a new key. I left the passphrase and all as blank when doing this. Not sure if that is the start of my issues. Now to the docker file.
I have tried multiple ways. Using the below docker file gives me an error when trying to do the actual git clone:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @
WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions 0755 for '/root/.ssh/id_rsa' are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. Load key "/root/.ssh/id_rsa": bad permissions git@bitbucket.org: Permission denied (publickey). fatal: Could not read from remote repository.Please make sure you have the correct access rights and the repository exists.
The docker file is:
FROM ubuntu:18.04 AS intermediate
ENV HOME /root
ARG DEBIAN_FRONTEND=noninteractive
VOLUME /home/user/.ssh/id_rsa /root/.ssh/id_rsa
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
apt-get update && apt-get upgrade -y && apt-get -y --no-install-recommends install \
build-essential \
cmake \
ssh \
git
RUN chmod 700 /root/.ssh #&& \#
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
RUN chmod 400 /root/.ssh/id_rsa
RUN git clone git@bitbucket.org:company/myRepo.git
FROM ubuntu:18.04
LABEL Description="Build environment"
ENV HOME /root
SHELL ["/bin/bash", "-c"]
ARG DEBIAN_FRONTEND=noninteractive
COPY --from=intermediate myRepo /git/myRepo
I have even tried adding the ssh key directly to the docker file and creating the id_rsa file and location. I get the same error except the permission is now 0644. Here is the other version of my docker file:
FROM ubuntu:18.04 AS intermediate
ENV HOME /root
ARG SSH_PRIVATE_KEY="ssh-rsa AAA..."
ARG DEBIAN_FRONTEND=noninteractive
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
apt-get update && apt-get upgrade -y && apt-get -y --no-install-recommends install \
build-essential \
cmake \
ssh \
git
RUN mkdir /root/.ssh/
RUN chmod 755 /root/.ssh
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa
RUN ssh-keygen -f ~/.ssh/id_rsa -p
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
RUN chmod 400 /root/.ssh/id_rsa
RUN git clone git@bitbucket.org:company/myRepo.git
FROM ubuntu:18.04
LABEL Description="Build environment"
ENV HOME /root
SHELL ["/bin/bash", "-c"]
ARG DEBIAN_FRONTEND=noninteractive
COPY --from=intermediate myRepo /git/myRepo
I followed instructions to set up the file to use an SSH key to clone the repo from here.
I was getting issues about formatting for the key. That led me to adding the ssh-keygen RUN command that supposedly will force proper formatting. This was found here.
As for solving the permission issue, I have seen plenty of threads about inserting chmod, but the permissions I set don't even seem to show up properly. I checked here to try and solve the permission issue.