Docker Community Forums

Share and learn in the Docker community.

Unable to clone private repository in docker container

I am trying to clone a private repository inside my docker container using ssh keys. I am using my own ssh keys by exporting it and then passing it in ARGS. Following is my docker file content

FROM python:3.6


# Add credentials on build

# making direcotry
RUN mkdir /root/.ssh/
# wrting ssh_key to system
RUN echo $SSH_PRIVATE_KEY >> /root/.ssh/id_rsa && chmod 600 /root/.ssh/id_rsa
#RUN chmod 600 /root/.ssh/id_rsa
# adding git agents to hosts
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan -T 60 >> /root/.ssh/known_hosts
# copying requirements
COPY ./requirements/private_requirements.txt /code/
# installing requirements
RUN pip install -r private_requirements.txt

But when i build it i am getting following error

—> Running in 65ef13750ea9
Collecting git+ssh:// (from -r private_requirements.txt (line 1))
Cloning ssh:// to /tmp/pip-req-build-3085swbp
Warning: Permanently added the RSA host key for IP address ‘’ to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Command “git clone -q ssh:// /tmp/pip-req-build-3085swbp” failed with error code 128 in None
You are using pip version 10.0.1, however version 18.0 is available.
You should consider upgrading via the ‘pip install --upgrade pip’ command.
ERROR: Service ‘connect’ failed to build: The command ‘/bin/sh -c pip install -r private_requirements.txt’ returned a non-zero code: 1

I am sure ARG SSH_PRIVATE_KEY has correct value and i have verified it by printing it. Please point out if there is a problem in my approach or suggest some other approach to achieve this. Thanks in advance.

1 Like

I’m having the same problem. Did you find a solution?