From 01fae90b3d69acb94f76b51eb453037fdf36bbcc Mon Sep 17 00:00:00 2001 From: a73x Date: Sun, 25 Aug 2024 10:04:00 +0100 Subject: init --- Dockerfile | 46 ++++++++++++++++++++++++++++++++++++++++++++++ entrypoint.sh | 12 ++++++++++++ init-repo.sh | 7 +++++++ 3 files changed, 65 insertions(+) create mode 100644 Dockerfile create mode 100644 entrypoint.sh create mode 100644 init-repo.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9667c52 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +# Use a base image with Git and OpenSSH installed +FROM alpine:latest + +# Install necessary packages +RUN apk add --no-cache git openssh + +# Create a directory for the Git repositories +RUN mkdir -p /git-server/repos + +# Create a user for running the Git server with git-shell as the default shell +RUN addgroup -S git && adduser -S git -G git -s /usr/bin/git-shell && echo "git:*" | chpasswd -e + +# Set the working directory +WORKDIR /git-server + +# Create SSH directory, authorized_keys file, and set permissions +RUN mkdir -p /home/git/.ssh \ + && chmod 700 /home/git/.ssh \ + && chown -R git:git /home/git/.ssh + +# Add your SSH public key and restrict to git-shell +RUN echo 'command="/usr/local/bin/git-init-repo.sh %r",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPQgoZvm5U8SKkjZr03YcGdbMDvx2DxuSfXBYjyBJzrE' > /home/git/.ssh/authorized_keys \ + && chmod 600 /home/git/.ssh/authorized_keys \ + && chown -R git:git /home/git/.ssh + +# Disable password authentication to enforce SSH key-based access +RUN echo "PasswordAuthentication no" >> /etc/ssh/sshd_config + +# Set MOTD + +run echo "Connection successful!" > /etc/motd + +# Expose SSH port +EXPOSE 22 + +COPY init-repo.sh /usr/local/bin/git-init-repo.sh +RUN chmod +x /usr/local/bin/git-init-repo.sh + +# Copy the entrypoint script +COPY entrypoint.sh /entrypoint.sh + +# Make the entrypoint script executable +RUN chmod +x /entrypoint.sh + +# Set the entrypoint +ENTRYPOINT ["/bin/sh","/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..77ae84d --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/bash +if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then + ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' + ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N '' + ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' + ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N '' +fi + + + +# Start the SSH daemon +/usr/sbin/sshd -D -e 2>&1 diff --git a/init-repo.sh b/init-repo.sh new file mode 100644 index 0000000..29cab4e --- /dev/null +++ b/init-repo.sh @@ -0,0 +1,7 @@ +#!/bin/bash +REPO_DIR="/git-server/repos/$1" +if [ ! -d "$REPO_DIR" ]; then + mkdir -p "$REPO_DIR" + cd "$REPO_DIR" + git init --bare +fi -- cgit v1.2.3