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 5 years have passed since last update.

VSCode 多段SSH Vagrant内のDocker Containerを編集

Posted at

sshconfig作成

C:\Users\ユーザーネーム.sshにconfigファイル作って下記を記述

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

powershellで下記実行

$ scp .vagrant/machines/default/virtualbox/private_key default:/home/vagrant/.ssh/

vagrantへログイン後、下記実行

$ vagrant ssh
# private_key の実行権限を変更
$ chmod 600 ~/.ssh/private_key

秘密鍵の転送

# 注意 `vagrant up` したディレクトリで実行する
> scp .vagrant/machines/default/virtualbox/private_key vagrant-os:/home/vagrant/.ssh/

# Guest OS に SSH接続
> vagrant ssh

# private_key の実行権限を変更(Guest OS 内)
$ chmod 600 ~/.ssh/private_key

Docker Container の起動

FROM ruby:2.5.1

# リポジトリを更新し依存モジュールをインストール
RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       nodejs

# ルート直下にwebappという名前で作業ディレクトリを作成(コンテナ内のアプリケーションディレクトリ)
RUN mkdir /webapp
WORKDIR /webapp

# ホストのGemfileとGemfile.lockをコンテナにコピー
ADD Gemfile /webapp/Gemfile
ADD Gemfile.lock /webapp/Gemfile.lock

# bundle installの実行
RUN bundle install

# ホストのアプリケーションディレクトリ内をすべてコンテナにコピー
ADD . /webapp

# puma.sockを配置するディレクトリを作成
RUN mkdir -p tmp/sockets


# ここから最後までがopenssh設定
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd

# /etc/ssh/sshd_config の設定書き換え
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# 公開鍵をコピー
COPY authorized_keys /root/authorized_keys

EXPOSE 22

# Activate authorized_keys & Boot sshd
CMD mkdir ~/.ssh && \
    mv ~/authorized_keys ~/.ssh/authorized_keys && \
    chmod 0600 ~/.ssh/authorized_keys &&  \
    # 最後に ssh を起動
    /usr/sbin/sshd -D

# 公開鍵をコピー
$ cp ~/.ssh/authorized_keys .

# build
$ docker build ./ -t example

# Docker Container を起動
# Port:10000 を Port:22 に転送
$ docker run -d -p 10000:22 example

# Docker Container に SSH 接続
$ ssh -o 'StrictHostKeyChecking no' root@127.0.0.1 -p 10000 -i ~/.ssh/private_key

powershellで確認

$ ssh -o 'StrictHostKeyChecking no' docker-os

.ssh/configファイル 編集

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

# 下記追記
Host docker-os
Hostname 127.0.0.1
User root
Port 10000
ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe -W %h:%p default
IdentityFile D:/vagrant/centos7/.vagrant/machines/default/virtualbox/private_key

VScodeを起動後
拡張機能からRemote Developmentインストール
リモートエクスプローラーでsshTargetsから編集

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?