Skip to content

Instantly share code, notes, and snippets.

@Rulexec
Created September 26, 2022 17:38
Show Gist options
  • Save Rulexec/04139bfa0e09bee3a88b1dfb7a3408bf to your computer and use it in GitHub Desktop.
Save Rulexec/04139bfa0e09bee3a88b1dfb7a3408bf to your computer and use it in GitHub Desktop.
#!/bin/bash
IMAGE_NAME="ubuntu-with-sshd"
CONTAINER_NAME="ubuntu-with-sshd"
SSH_PORT=9022
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [[ "$(docker images -q $IMAGE_NAME 2> /dev/null)" == "" ]]; then
cat > $DIR/Dockerfile <<- EOM
FROM ubuntu:22.04
RUN apt update && apt install -y openssh-server python3 && \\
(echo "chown -R root:root /root/.ssh /etc/ssh && service ssh start" >> /root/init-file)
EOM
docker build -t $IMAGE_NAME $DIR
fi
if [ ! -d "$DIR/.ssh" ]
then
mkdir $DIR/.ssh
cp ~/.ssh/id_rsa.pub $DIR/.ssh/authorized_keys
fi
if [ ! -d "$DIR/ssh" ]
then
docker run --name $CONTAINER_NAME $IMAGE_NAME
docker cp $CONTAINER_NAME:/etc/ssh $DIR/ssh
docker rm $CONTAINER_NAME
fi
docker run -it --rm --name $CONTAINER_NAME \
-p $SSH_PORT:22 \
-p 9080:80 \
-v $DIR/.ssh:/root/.ssh \
-v $DIR/ssh:/etc/ssh \
$IMAGE_NAME /bin/bash --init-file /root/init-file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment