From be4c88df011bb926ba4048e98d69dd5a9147c188 Mon Sep 17 00:00:00 2001 From: Tom Davidson Date: Tue, 16 May 2023 11:40:31 +0100 Subject: [PATCH 1/3] Allow override of private key, docker image and docker command to make script more portable. Passing a $1 param to the script loads that private key from the ~/.ssh diri instead of the default id_rsa. The image on Docker Hub is suggested in the comment for an alternative to having built your own image. If you are using a different CRI, you should be able to override the docker command with the one you are using. Also some minor word tweaks. --- run.sh | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/run.sh b/run.sh index 24cb513..f5fb686 100755 --- a/run.sh +++ b/run.sh @@ -28,18 +28,27 @@ cyan='\033[0;36m' red='\033[0;31m' nc='\033[0m' +# To override the default docker command e.g. to use podman +# export the following environment variable +docker=${docker:-docker} + +# To override the default and use the docker hub image, +# uncomment or export the following environment variable +# N.B. you will need to have previously done a docker pull of the image +# image=nardeas/ssh-agent + # Find image id -image=$(docker images|grep docker-ssh-agent|awk '{print $3}') +image=$($docker images|grep ${image:-docker-ssh-agent}|awk '{print $1}') # Find agent container id -id=$(docker ps -a|grep ssh-agent|awk '{print $1}') +id=$($docker ps -a|grep ssh-agent|awk '{print $1}') # Stop command if [ "$1" == "-s" ] && [ $id ]; then echo -e "Removing ssh-keys..." - docker run --rm --volumes-from=ssh-agent -it docker-ssh-agent:latest ssh-add -D + $docker run --rm --volumes-from=ssh-agent -it ${image} ssh-add -D echo -e "Stopping ssh-agent container..." - docker rm -f $id + $docker rm -f $id exit fi @@ -47,21 +56,21 @@ fi if [ -z $image ]; then echo -e "${bold}The image for docker-ssh-agent has not been built.${nc}" echo -e "Building image..." - docker build -t docker-ssh-agent:latest -f Dockerfile . + $docker build -t docker-ssh-agent:latest -f Dockerfile . echo -e "${cyan}Image built.${nc}" fi -# If container is already running, exit. +# If container is already present, exit. if [ $id ]; then - echo -e "A container named 'ssh-agent' is already running." - echo -e "Do you wish to stop it? (y/N): " + echo -e "A container named 'ssh-agent' is already present." + echo -e "Do you wish to stop and remove it? (y/N): " read input if [ "$input" == "y" ]; then echo -e "Removing SSH keys..." - docker run --rm --volumes-from=ssh-agent -it docker-ssh-agent:latest ssh-add -D + $docker run --rm --volumes-from=ssh-agent -it ${image} ssh-add -D echo -e "Stopping ssh-agent container..." - docker rm -f $id + $docker rm -f $id echo -e "${red}Stopped.${nc}" fi @@ -70,9 +79,9 @@ fi # Run ssh-agent echo -e "${bold}Launching ssh-agent container...${nc}" -docker run -d --name=ssh-agent docker-ssh-agent:latest +$docker run -d --name=ssh-agent ${image} echo -e "Adding your ssh keys to the ssh-agent container..." -docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh -it docker-ssh-agent:latest ssh-add /root/.ssh/id_rsa +$docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh:ro -it ${image} ssh-add /root/.ssh/${1:-id_rsa} echo -e "${green}ssh-agent is now ready to use.${nc}" From 3d2b84ce03da630dc8298611bc4a07698b3c7105 Mon Sep 17 00:00:00 2001 From: Tom Davidson Date: Thu, 27 Feb 2025 08:45:55 +0000 Subject: [PATCH 2/3] force amd64 to be accepted to suppress warnings replace ~ with $HOME for more reliable operation --- run.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/run.sh b/run.sh index f5fb686..2e623ff 100755 --- a/run.sh +++ b/run.sh @@ -32,6 +32,11 @@ nc='\033[0m' # export the following environment variable docker=${docker:-docker} +# force amd64 to be accepted to suppress warnings +# https://stackoverflow.com/questions/69054921/docker-on-mac-m1-gives-the-requested-images-platform-linux-amd64-does-not-m +# setting it to arm64 may fail if there is no ARM image +DOCKER_DEFAULT_PLATFORM=linux/amd64 + # To override the default and use the docker hub image, # uncomment or export the following environment variable # N.B. you will need to have previously done a docker pull of the image @@ -82,6 +87,6 @@ echo -e "${bold}Launching ssh-agent container...${nc}" $docker run -d --name=ssh-agent ${image} echo -e "Adding your ssh keys to the ssh-agent container..." -$docker run --rm --volumes-from=ssh-agent -v ~/.ssh:/.ssh:ro -it ${image} ssh-add /root/.ssh/${1:-id_rsa} +$docker run --rm --volumes-from=ssh-agent -v $HOME/.ssh:/.ssh:ro -it ${image} ssh-add /root/.ssh/${1:-id_rsa} echo -e "${green}ssh-agent is now ready to use.${nc}" From e41a520cb6c5ec863b44a04d76a0202384b8fc4e Mon Sep 17 00:00:00 2001 From: Tom Davidson Date: Tue, 6 May 2025 10:17:15 +0100 Subject: [PATCH 3/3] support overriding the container name so multiple can be run in parallel, should they be required --- run.sh | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/run.sh b/run.sh index 2e623ff..2e308d9 100755 --- a/run.sh +++ b/run.sh @@ -37,6 +37,11 @@ docker=${docker:-docker} # setting it to arm64 may fail if there is no ARM image DOCKER_DEFAULT_PLATFORM=linux/amd64 +# To override the default and use your own container name, +# uncomment or export the following environment variable +# N.B. you will need to have previously done a docker pull of the image +name=${name:-ssh-agent} + # To override the default and use the docker hub image, # uncomment or export the following environment variable # N.B. you will need to have previously done a docker pull of the image @@ -46,13 +51,13 @@ DOCKER_DEFAULT_PLATFORM=linux/amd64 image=$($docker images|grep ${image:-docker-ssh-agent}|awk '{print $1}') # Find agent container id -id=$($docker ps -a|grep ssh-agent|awk '{print $1}') +id=$($docker ps -a|grep $name|awk '{print $1}') # Stop command if [ "$1" == "-s" ] && [ $id ]; then echo -e "Removing ssh-keys..." - $docker run --rm --volumes-from=ssh-agent -it ${image} ssh-add -D - echo -e "Stopping ssh-agent container..." + $docker run --rm --volumes-from=$name -it ${image} ssh-add -D + echo -e "Stopping $name container..." $docker rm -f $id exit fi @@ -67,14 +72,14 @@ fi # If container is already present, exit. if [ $id ]; then - echo -e "A container named 'ssh-agent' is already present." + echo -e "A container named '$name' is already present." echo -e "Do you wish to stop and remove it? (y/N): " read input if [ "$input" == "y" ]; then echo -e "Removing SSH keys..." - $docker run --rm --volumes-from=ssh-agent -it ${image} ssh-add -D - echo -e "Stopping ssh-agent container..." + $docker run --rm --volumes-from=$name -it ${image} ssh-add -D + echo -e "Stopping $name container..." $docker rm -f $id echo -e "${red}Stopped.${nc}" fi @@ -83,10 +88,10 @@ if [ $id ]; then fi # Run ssh-agent -echo -e "${bold}Launching ssh-agent container...${nc}" -$docker run -d --name=ssh-agent ${image} +echo -e "${bold}Launching $name container...${nc}" +$docker run -d --name=$name ${image} -echo -e "Adding your ssh keys to the ssh-agent container..." -$docker run --rm --volumes-from=ssh-agent -v $HOME/.ssh:/.ssh:ro -it ${image} ssh-add /root/.ssh/${1:-id_rsa} +echo -e "Adding your ssh keys to the $name container..." +$docker run --rm --volumes-from=$name -v $HOME/.ssh:/.ssh:ro -it ${image} ssh-add /root/.ssh/${1:-id_rsa} -echo -e "${green}ssh-agent is now ready to use.${nc}" +echo -e "${green}$name is now ready to use.${nc}"