Search code examples
gitdockersshssh-keys

How to import or add bitbucket rsa keys from users machine so that git clone works?


I am trying to clone a private repo from a private bitbucket repository. However, my docker build fails since I cannot import rsa key. What is the correct solution here? I am planning to distribute this docker file so that each user can build the docker image. I can see that my image hasn't done the git clone.

DockerFile

FROM debian:9
MAINTAINER jam.gord@yw.com

ARG SSH_PRIVATE_KEY

RUN apt-get update -y

# Update, upgrade and install
RUN apt-get install -y gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat cpio python python3 python3-pip python3-pexpect xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev pylint3 xterm

# Install software 
RUN apt-get install -y git

RUN mkdir -p ~/.ssh && umask 0077 && echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa \


RUN mkdir -p /home/jam \
    cd /home/jam \
    git clone ssh://git@bitbucket.yw.com:7999/ba/yw-linux-build.git

#Set working directory
WORKDIR /home/jam

Solution

  • use COPY in dockerfile to import rsa key from the host.

    COPY id_rsa.pub /home/jam/.ssh
    COPY id_rsa /home/jam/.ssh
    

    id_rsa.pub and id_rsa file located in the folder same as dockerfile.

    OR

    echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa \ 
    

    after this ,remember to set the permisson 600 of id_rsa