sshだけのコンテナ(改)

に公開

はじめに

  • sshd だけのコンテナをつくる
  • ベースは debian を使用
  • google compute engine みたいな感じで使いたい
  • ユーザを作成する
  • ユーザ固有の設定はシェルへ
  • シェルをコンテナ作成時に実行する

環境

i5-14400

  • windows11 pro
  • powershell 7
  • wsl2 ubuntu24.04
  • sshd (apt)
  • docker (snap)

i5-10400

  • windows11 home
  • powershell 7
  • wsl2 ubuntu24.04 ( いらなかったかも? )
  • vscode

操作

i5-14400 (win11 pro)

  • home からリモートデスクトップで入っておく
  • wsl2 のターミナルを立ち上げておく
  • wsl2 の ubuntu の sshd を自動起動して常駐させておく方法はしらない
  • .ssh/authorized_keys に home 側で作成したキーを登録 (wsl2)
  • .ssh/id_ed25519 id_ed25519.pub 作っておく (wsl2)
  • wsl2 上では docker 関連しか動かさない感じ

i5-10400 (win11 home)

  • vscode の remote-ssh で pro 側の ubuntu に入る
  • 操作はすべて vscode から行う
  • container-tools 拡張機能を入れる (remote 接続先の ubuntu にインストールされる)
  • docker 操作はすべて vscode で行えるはず
  • .ssh/id_ed25519 id_ed25519.pub 作っておく (powershell 7)
  • vscode で使う .ssh/config も作っておく (powershell 7)

sandobox

  • home の vscode から コンテナを作成する (今回の ssh02 や go 開発環境など)
  • .ssh/config に コンテナへの接続情報を追記する
  • remote-ssh で pro の ubuntu に接続しているのとは別に
    remote-ssh で 直接コンテナに接続する (今回は ssh02)
  • コンテナ何でいろいろする。
  • go開発環境の場合は、remote-ssh したあと、goの拡張機能と dlv? dvl? を使ってデバッグとかする

compose

compose.yaml
services:
  ssh:
    image: ssh02
    build:
      context: ./ssh
      dockerfile: Dockerfile
    container_name: ssh02
    hostname: ssh02
    ports:
      - "2222:22"
    # command: tail -f /dev/null

dockerfile

dockerfile
FROM debian:bookworm-slim

RUN apt-get update \
&&  apt-get install -y --no-install-recommends \
    sudo \
    openssh-server \
    git \
    vim \
&&  apt-get clean \
&&  rm -rf /var/lib/apt/lists/*

RUN mkdir -p /run/sshd \
&&  echo "PubkeyAuthentication yes" > /etc/ssh/sshd_config.d/99-custom.conf \
&&  ssh-keygen -A

RUN useradd -m -s /bin/bash yukip \
&&  usermod -aG sudo yukip \
&&  echo "yukip ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

COPY --chown=yukip:yukip init.sh /home/yukip/

RUN su - yukip -c ". ~/init.sh > init.log 2>&1"

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D", "-e"]

shell

init.sh
#!/bin/bash
set -vx

#INIT START

umask 077

mkdir ~/.ssh

cat <<'END' | sed 's/^ \{4\}//' > ~/.ssh/authorized_keys
    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOLmB/QNbTW+kSFVbbpRnMhelEbZWWOMqxc68B9/mHAi yukip@i5-10400
    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM6uTTorSMRo4pxaFZLjXkjdJvSwc9MoeW4pjdzkoe4O yukip@i5-14400
END

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C "yukip@container" -N "" -q

git config --global user.name "yukip"
git config --global user.email "yukip@chottodake.dev"

cat <<'END' | sed 's/^ \{4\}//' > ~/yukip.bashrc
    #env
    export TZ=Asia/Tokyo

    #history
    shopt -s histverify
    alias hist='history  | grep -v "hist " | grep --color=never'

    #etc
    alias ls='ls --color=always'

    #git
    alias gitlog='git fetch;git log --oneline --graph --all origin/main'
    alias gitcommit='git add .;git commit -m '
    alias gitpush='git push;git push --tags'
    alias gitdiff='git fetch;git diff --name-status main origin/main'

    # prompt
    PS1='\n\[\e[1;32m\]\h [$(date +%Y/%m/%d) \t \w]\n\$\[\e[0m\] '
END

echo "source ~/yukip.bashrc" >> /home/yukip/.bashrc

#INIT END

home .ssh/config

Host i5-14400
    HostName 192.168.1.18
    User yukip
Host ssh
    HostName 192.168.1.18
    User yukip
    Port 2222
# Host con01
#     HostName 192.168.1.18
#     User yukip
#     Port 2221

Discussion