Skip to content

Instantly share code, notes, and snippets.

@YumingChang02
Created October 31, 2023 12:38
Show Gist options
  • Save YumingChang02/4cf17d29fa939005996a89828b78de98 to your computer and use it in GitHub Desktop.
Save YumingChang02/4cf17d29fa939005996a89828b78de98 to your computer and use it in GitHub Desktop.
use ssh key to clone git in Dockerfile
#!/bin/bash

function handler(){
    echo "Processing the Ctrl+C, trying to kill ssh agent"
    ssh-agent -k
    exit 0
}

trap handler SIGINT

echo "starting ssh agent..."
eval `ssh-agent` && \
echo "adding your private keys..."
ssh-add && \
echo "start building..."
DOCKER_BUILDKIT=1 docker build --ssh default=$SSH_AUTH_SOCK . -t ${1} --no-cache;
ssh-agent -k

in Dockerfile

RUN --mount=type=ssh \
    mkdir -p -m 0600 ~/.ssh && ssh-keyscan [email protected] >> ~/.ssh/known_hosts && \
    git clone xxxxx.git \
    rm -rf ~/.ssh <your repo if you don't want to keep>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment