Docker + SSH + Git Clone Isssue

I have read so many issues about the same exact thing tonight, but I’ll be damned if any of the solutions actually worked for.

Simply put, I need to clone a private Git repo hosted on GitHub to my docker image.

  • bind mounting a volume in docker fails with lxc-start: No such file or directory
  • Docker - Disk Quotas
  • Update shared volume from data container
  • why vagrant 1.6 + docker keep asking password for ssh connection
  • Mounting a dir in docker does not work when dir is linked with symlink?
  • How can I run elasticsearch on a docker container using supervisor?
  • This is what I have thus far in the Dockerfile:

    FROM debian:wheezy
    ENV DEBIAN_FRONTEND noninteractive
    
    # Update aptitude with new repo
    RUN apt-get update
    
    # Install software 
    RUN apt-get install -y \ 
        # All of my packages here...
    
    # Make ssh dir
    RUN mkdir /root/.ssh/
    
    # Copy over private key, and set permissions
    ADD ssh/id_rsa /root/.ssh/id_rsa
    RUN chmod 700 /root/.ssh/id_rsa
    RUN touch /root/.ssh/known_hosts
    
    # Add GithHubs key
    RUN ssh-keyscan -T 60 github.com >> /root/.ssh/known_hosts
    
    # Create the Development directory and then move into the directory.
    RUN mkdir -p /var/www/dev
    WORKDIR /var/www/dev
    
    # Start-up Git and pull in the Dev branch.
    RUN ssh -v git@github.com
    #RUN git init
    #RUN git remote add origin git@github.com:<my_git_repo>
    #RUN git fetch
    #RUN git checkout -t origin/dev
    #RUN git clone git@github.com:<my_git_repo>
    

    ssh -v gives me the following debug log:

    OpenSSH_6.0p1 Debian-4+deb7u4, OpenSSL 1.0.1e 11 Feb 2013
    Pseudo-terminal will not be allocated because stdin is not a terminal.
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug1: Connecting to github.com [192.30.252.131] port 22.
    debug1: Connection established.
    debug1: permanently_set_uid: 0/0
    debug1: identity file /root/.ssh/id_rsa type -1
    debug1: identity file /root/.ssh/id_rsa-cert type -1
    debug1: identity file /root/.ssh/id_dsa type -1
    debug1: identity file /root/.ssh/id_dsa-cert type -1
    debug1: identity file /root/.ssh/id_ecdsa type -1
    debug1: identity file /root/.ssh/id_ecdsa-cert type -1
    debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
    debug1: no match: libssh-0.7.0
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4+deb7u4
    debug1: SSH2_MSG_KEXINIT sent
    debug1: SSH2_MSG_KEXINIT received
    debug1: kex: server->client aes128-ctr hmac-sha1 none
    debug1: kex: client->server aes128-ctr hmac-sha1 none
    debug1: sending SSH2_MSG_KEX_ECDH_INIT
    debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
    debug1: Host 'github.com' is known and matches the RSA host key.
    debug1: Found key in /root/.ssh/known_hosts:1
    Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
    debug1: ssh_rsa_verify: signature correct
    debug1: SSH2_MSG_NEWKEYS sent
    debug1: expecting SSH2_MSG_NEWKEYS
    debug1: SSH2_MSG_NEWKEYS received
    debug1: SSH2_MSG_SERVICE_REQUEST sent
    debug1: SSH2_MSG_SERVICE_ACCEPT received
    debug1: Authentications that can continue: publickey
    debug1: Next authentication method: publickey
    debug1: Trying private key: /root/.ssh/id_rsa
    debug1: key_parse_private_pem: PEM_read_PrivateKey failed
    debug1: read PEM private key done: type <unknown>
    debug1: read_passphrase: can't open /dev/tty: No such device or address
    debug1: Trying private key: /root/.ssh/id_dsa
    debug1: Trying private key: /root/.ssh/id_ecdsa
    debug1: No more authentication methods to try.
    Permission denied (publickey).
    

    I have tried the option of setting StrictHostChecking to no. I have tried a separate config file under the SSH directory to specify host, port, identityfile (being the private key, not the public one).

    What am I missing here? The key on the VM that is created is precisely the same as what I have on my local machine.

    I am sure whatever I am missing is really simple.

    Thanks for your help!

  • CoreOS + Docker - Assign Containers a pool of routable IP's
  • Docker container not starting (docker start)
  • Docker Centos 7 easy_install supervisor ssl issue behind proxy
  • Docker 1.12 reads old line from yml file “volumes” instead of new one
  • Docker: Cannot locate specified Dockerfile error
  • Why does not work docker command in MacOS?
  • 2 Solutions collect form web for “Docker + SSH + Git Clone Isssue”

    It might be better to use a GitHub personal access token instead of your ssh key.

    https://help.github.com/articles/creating-an-access-token-for-command-line-use/

    This removes the need for you to bake your ssh key into the image, which is more secure, and it allows clones over https, which should simplify your dockerfile. If you need to revoke the token, it is easy to do from their website, and you don’t need to replace your personal ssh key everywhere.

    If you looked at this, and can’t use this option, let me know and I can help you figure out the ssh key issue.

    One of the problems could be that the .ssh/config file needs to have specific permissions. Trying using permissions 600 i.e. rw- --- --- instead of 700 i.e. rwx --- ---.

    Tip for debugging

    You can try to execute the following inside the container to make sure that the setup works:

    ssh -T git@github.com
    

    If everything is setup properly, you should see:

    Hi username! You've successfully authenticated, but GitHub does not provide shell
    access.
    

    Alternate Strategy for Cloning Private Repositories

    When it comes cloning private repositories inside docker, I use the ssh agent session on the local host inside the container, by mounting the relevant socket. Details can be found here: https://ahmadnazir.github.io/posts/2016-06-24-accessing-private-repos-in-docker.html

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