add ssh keys in Docker

in my Dockefile i am adding ssh key to the docker and cloning a project from bitbucket.
i can pull another branch in the Docker file easily.

ARG key
ARG pub_key
RUN mkdir /root/.ssh/
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
ADD $key /root/.ssh/
ADD $pub_key /root/.ssh/
RUN git clone git@bitbucket.org:******************/sql.git
WORKDIR "/sql"
RUN git pull origin testBranch

the repo is cloned sucesfully and and a pull is made successfully from the testBranch
when i run this docker using docker run command and try any git command it says

  • Routing to different container in docker using zuul not working
  • share CUDA library with docker container
  • How to set default docker environment variables
  • What does 'docker inspect -f .' do?
  • Where to put ebextensions config in AWS Elastic Beanstalk Docker deploy with dockerrun source bundle?
  • How to keep the docker container up and running?
  • Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    

    but the ssh key is present in the directory /root/.ssh

  • stub.GetHistoryKeys() reporting GetHistoryKeys() function undefined. when tried go build on my Chaincode
  • Docker - How to get the name (user/repo:tag) of the base image used to build another image
  • Docker: Run an arbitrary command from inside a priveleged container on the host
  • Postgres image from docker can't find initdb. What am I missing?
  • Committing an interactive container in docker
  • Understanding docker diff
  • One Solution collect form web for “add ssh keys in Docker”

    Your key is added against root user (/root/.ssh/). I guess when you run docker run you are switching to another user (maybe, docker).

    If you add your id_rsa.pub to correct user (the user after running docker run command) then it should work.

    $ whoami show you the current user.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.