0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Dockerでsshサーバ構築

Last updated at Posted at 2021-04-23

"相手サーバにsshでログインして・・・"という実装の動作確認をローカル開発環境で完結させたい時に。

  1. 鍵ファイルを作っておく

    cd /path/to/project/local-ssh-server
    ssh-keygen -t rsa -b 4096 -C "localtest@example.com" -f 
    id_rsa_localtest
    mv id_rsa_localtest.pub authorized_keys
    
  2. /path/to/project/local-ssh-server/Dockerfileを作る

    FROM amazonlinux:2
    RUN yum install -y shadow-utils \
        && yum install -y sudo \
        && yum install -y openssh-server
    RUN useradd localtest
    RUN mkdir /home/localtest/.ssh && chmod 700 /home/localtest/.ssh
    ADD authorized_keys /home/localtest/.ssh/authorized_keys
    RUN chmod 600 /home/localtest/.ssh/authorized_keys
    RUN chown -R localtest:localtest /home/localtest/.ssh
    WORKDIR /home/localtest/
    

    amazonlinux:2なのは、使い慣れてるから、というだけの理由です。

  3. /path/to/project/Dockerfileに以下を追加

    sshクライアント
    = 開発プログラム稼働サーバ
    => /path/to/project/Dockerfileでビルド
    を想定。

    RUN mkdir /root/.ssh && chmod 700 /root/.ssh
    ADD ./local-ssh-server/id_rsa_localtest /root/.ssh/id_rsa_localtest
    
  4. /path/to/project/docker-compose.ymlを更新

    local-ssh-server:以下を追加

    version: '3'
    services:
      project_name:
        container_name: "project_name"
        build: ./
      ・
      ・
      ・
      local-ssh-server:
        container_name: "local-ssh-server"
        build: ./local-ssh-server/
        privileged: true
        environment:
          TZ: Asia/Tokyo
        ports:
          - "22:22"
        command: /sbin/init
    
  5. 確認

    cd /path/to/project/
    docker-compose up -d --build
    docker exec -it project_name /bin/bath
    

    project_nameコンテナにログインして、以下でssh接続できればOK!

    ssh -i /root/.ssh/id_rsa_localtest localtest@local-ssh-server
    
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?