dockerfileでSSHの設定も自動化する

2020年12月22日

dockerfileレベルの話にはなりますが、環境構築はコード化しておきましょう。
特にSSHの設定は、セキュリティ的に最低限必要なのでコードで管理しておくと楽です。

関連資料:
  ・dockerのコマンドはこちら
  ・dockerfileのRUNとCMDの違いはこちら

dockerfile


FROM centos:7
MAINTAINER Houdoukyokucho

#yumのアップデートとクリーン
RUN yum -y update && yum clean all
#sshサーバとクライアントのインストール
RUN yum -y install openssh-server openssh-clients
#ssh設定変更
RUN sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
#user作成
RUN useradd -m testuser
#鍵の設定
#※windowsのlocalhostから公開鍵をコピーするときは相対パスを指定する
RUN mkdir /home/testuser/.ssh
RUN chown testuser:testuser /home/testuser/.ssh
RUN chmod 700 /home/testuser/.ssh
COPY authorized_keys /home/testuser/.ssh/authorized_keys
RUN chown testuser:testuser /home/testuser/.ssh/authorized_keys
RUN chmod 600 /home/testuser/.ssh/authorized_keys

CMD /sbin/init

EXPOSE 80
EXPOSE 443
EXPOSE 22

docker-compose


version: '3'
services:
  test:
    image: test_image:01
    container_name: test_container
    privileged: true
    #自動起動
    restart: always
    #ポートフォワード
    ports:
      - "127.0.0.1:8080:80"
      - "xxx.xxx.xxx.xxx:10022:22"
YouTube

2020年12月22日