LoginSignup
0
0

More than 1 year has passed since last update.

RaspberryPiOSの動くDockerコンテナにgitサーバーを構築する

Last updated at Posted at 2022-07-15

やりたいこと

Dockerコンテナにgitサーバーを構築する。

まずは、RaspberryPiOSイメージを作る。

手順

1.構築用のコンテナを作る

docker create --name sshgitbuild01 -it raspios:0.0.1 /bin/bash
docker start sshgitbuild01
docker exec -it sshgitbuild01 /bin/bash

2.コンテナに入ってgit用のディレクトリを作る

mkdir /data
mkdir /data/app

3.コンテナに入ってgitをインストールする

apt update
apt install git

4.コンテナに入ってsshdが動くように設定

ssh-keygen -A
mkdir /run/sshd
chmod 755 /run/sshd

以下のファイルの行を修正

/etc/ssh/sshd_config
Port 2222

5.ホスト側でgit用のディレクトリを作成

sudo mkdir /data/app/main/git
sudo chown pi /data/app/main/git
sudo chgrp pi /data/app/main/git
sudo mkdir /data/app/main/git/root
sudo chgrp users /data/app/main/git/root
sudo chmod 775 /data/app/main/git/root

6.ホスト側でホーム用ディレクトリを作成

mkdir /data/app/main/git/root/home
chown pi /data/app/main/git/root/home
chgrp pi /data/app/main/git/root/home
chmod 755 /data/app/main/git/root/home

※正しい権限でhomeを作ってあげないと、sshの公開鍵が認識されない。

7.コンテナに入って、公開鍵をホスト側で管理できるようにするためにコンテナ側のホームディレクトリを/data/app/root/homeにする

cp ~/* /data/app/root/home
sudo usermod pi -d /data/app/root/home

8.一旦コンテナを停止してimageとして保存

docker stop sshgitbuild01
docker commit sshgitbuild01 raspios_sshgit:0.0.1

9.上で作ったimageを起動

docker create --name sshgitcont01 -t -v /data/app/main/git:/data/app -p 2222:2222 \
    -e LANG=C.UTF-8 \
    -e TZ=Asia/Tokyo \
raspios_sshgit:0.0.1 /usr/sbin/sshd -D -e

※-Dオプションはデーモン化しない
※-eオプションはログを標準出力に出す

10.公開鍵を/data/app/root/home/.ssh/authorized_keysに保存する

ssh pi@192.178.10.220 -p 2222
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
echo [公開鍵] >> ~/.ssh/authorized_keys

※正しい権限でディレクトリ/ファイルを作ってあげないとsshに認識されない。

11.再起動ポリシーを設定する

12.gitリポジトリの作成

mkdir /data/app/root/repos
sudo chown root /data/app/root/repos
sudo chgrp users /data/app/root/repos
sudo chmod 775 /data/app/root/repos
mkdir /data/app/root/repos/playground.git
cd /data/app/root/repos/playground.git
git int --bare 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0