LoginSignup
0
0

More than 3 years have passed since last update.

リモートサーバー上のDocker ContainerにVSCodeで入る

Last updated at Posted at 2021-04-19

0. まとめ

1. 秘密鍵と公開鍵の作成及び配置
2. Dockerのインストール
3. VSCodeでの設定
4. サーバー上でコンテナを建てる
5. コンテナにアクセス

1. 秘密鍵と公開鍵の作成及び配置

1-1. 秘密鍵と公開鍵の作成

local_host
$ cd ~/.ssh
$ ssh-keygen -t rsa -f keyname

keynameは好きな名前を入力する。
Enter file in which to save the key (/home/username/.ssh/keyname)と聞かれるので、そのままEnterを押せば、()の中のパスに保存される。
Enter passphrase (empty for no passphrase)でpassphraseを入力する(しなくても可)

1-2. 公開鍵の配置及び設定

先ほど作成した公開鍵の内容をサーバー側に保存する。
~/.ssh/authorized_keysにkeyname.pubの中身を追記しましょう。

remote_server
# ログイン
ssh hoge_user@hogehoge.hoge.com

# フォルダとファイルを作る(既に存在している場合は実行しなくても可)
mkdir .ssh
touch .ssh/authorized_keys

# 権限の設定。個々を間違えると、公開鍵認証でログイン出来ないので注意
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

# authorized_keysへ公開鍵の内容を追記。ほかの方法でも可
cat ~/.ssh/id_rsa.pub | ssh hoge_user@hogehoge.hoge.com 'cat >> .ssh/authorized_keys'

1-3. configの設定

~/.ssh/configに以下を追記

~/.ssh/config
Host xxx
    HostName xxx.xxx.xxx.xxx
    User username
    IdentityFile ~/.ssh/keyname

1-4. ssh-agentの設定

以下に従い、ssh-agentを設定しましょう
https://code.visualstudio.com/docs/containers/ssh#_set-up-ssh-tunneling

2. Dockerのインストール

ローカルにDocker Desktopをインストールしましょう。windows10の人はWSL2もインストールしましょう。

3. VSCodeでの設定

VSCodeに拡張機能のRemote DevelopmentDockerをインストール後、setting.jsonに以下を追記。

setting.json
"docker.host":"ssh://your-remote-user@your-remote-machine-fqdn-or-ip-here"

your-remote-userにはsshのユーザ名を、your-remote-machine-fqdn-or-ip-hereにはサーバーのipアドレス等を入力する。

4. サーバー上でコンテナを建てる

サーバー上でdocker runしましょう

5. コンテナにアクセス

VSCode画面左のDockerマークをクリックし、自分のコンテナにAttach Visual Studio Codeしましょう

参照

https://code.visualstudio.com/docs/remote/containers-advanced#_developing-inside-a-container-on-a-remote-docker-host
https://qiita.com/sakanashi/items/542ae428d9de9c7c59dc#%E3%82%84%E3%82%8A%E3%81%9F%E3%81%84%E3%81%93%E3%81%A8
https://nekodeki.com/ssh-config%E3%81%A8ssh-agent%E3%81%A7ssh%E6%8E%A5%E7%B6%9A%E3%82%92%E7%B0%A1%E5%8D%98%E3%81%AB%E3%81%99%E3%82%8B/#ssh-agent-2
https://qiita.com/ir-yk/items/af8550fea92b5c5f7fca

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