Search code examples
dockergithubmakefiledockerfilessh-keys

id_rsa invalid format in Dockerfile when using Makefile


I am attempting to pass an SSH key into a Dockerfile so I can pull down private repos from Git.

It works when I use this on the command line

export SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)"
docker build --build-arg SSH_PRIVATE_KEY  --tag image:latest .

Below is a snippet of my Dockerfile

ARG SSH_PRIVATE_KEY

RUN apt-get update && apt-get install -y git && apt-get install -y nano && \
    apt-get update && apt-get install -y python3.7 python3-pip python3.7-dev && \
    rm -rf /var/lib/apt/lists/*

RUN mkdir -p ~/.ssh && umask 0077 && echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa \
    && git config --global url."git@github.com:".insteadOf https://github.com/ \
    && ssh-keyscan github.com >> ~/.ssh/known_hosts

However when I try and run it from a makefile like so

SSH_PRIVATE_KEY=$(shell cat ~/.ssh/id_rsa)


build-image:
    docker build --build-arg SSH_PRIVATE_KEY="${SSH_PRIVATE_KEY}" --tag image:latest -f ./docker/Dockerfile .

I get the error

Load key "/root/.ssh/id_rsa": invalid format

Am I doing anything obviously wrong here?

Thanks!


Solution

  • The problem is SSH_PRIVATE_KEY=$(shell cat ~/.ssh/id_rsa). Make converts newlines from shell outputs to spaces. Use

    SSH_PRIVATE_KEY=`cat ~/.ssh/id_rsa`
    

    instead.