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 >> /root/.ssh/known_hosts
ADD $key /root/.ssh/
ADD $pub_key /root/.ssh/
RUN git clone******************/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

  • Get Elasticache address from within a docker Elastic Beanstalk container
  • Random GET /nginx_status call from inside docker container
  • pg_dump issue in docker
  • How do I write a unit test to check for correct behavior under conditions of transient system resource constraints?
  • How to forward data from a host computer to a Docker container?
  • Whats exactly mean by build, ship and run any app, anywhere with Docker
  • 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

  • Get debug informations from docker container which runs a tomcat server
  • Docker Container to Container communication with IPv6 only
  • External access to Node.JS app, within Docker container
  • Docker access localhost
  • Docker - invalid sharing of index.html Nginx file
  • Want to run docker in windows server 2016 on azure
  • 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 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.