使用 VSCODE 連接遠程服務器上的容器

自從 VSCODE 出現以來,我就立馬從 pycharm 轉入了。厭倦了 pycharm 的笨重,用了 vscode 之後只能說是真香,編輯器界的 flask。但是和 flask 一樣,雖然輕便,但是自然基本上一切都需要你自己去配置,各種插件和擴展。有些功能 pycharm 可能自帶,但是 VSCODE 就需要自己各種折騰,比如說本文的主題:本地連接遠程服務器上的容器

先說需求:使用 VSCODE 在本地(如 Windows)連接遠程服務器(如 Linux)上的容器,可以在編輯器內進行代碼修改等操作。

pycharm 學習來的偏方

經過與同事交流,發現 pycharm 實現這個需求的方法無非就是把容器的 22 端口映射出來然後使用 ssh 登陸,相當於把容器也當成一個虛擬化的遠程服務器。得益於 Remote - SSH,這在 VSCODE 中很好實現:

  1. 【遠程服務器】啓動容器,注意需要把容器的 22 端口映射出來,例如映射到 host 的 5222 端口:-p 5222:22

  2. 【遠程服務器容器】設置 root 賬戶密碼:

    passwd root
    

    根據提示設置密碼即可。如果你啓動容器的時候使用 -u 參數指定了一個非 root 用戶,那麼相應的要設置該賬戶的密碼。

  3. 【遠程服務器容器】很多鏡像都不會默認安裝 ssh,所以需要在容器內安裝 ssh 服務:

    apt update && apt install -y --no-install-recommends openssh-server
    
  4. 【遠程服務器容器】一般進入容器時使用的都是 root 賬號,但是 ssh 默認是禁止 root 賬號使用密碼遠程登錄的,所以需要修改 ssh 配置文件使其允許:

    sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
    

    但是如果你啓動容器的時候使用 -u 參數指定了一個非 root 用戶,那麼這步可以跳過。

  5. 【遠程服務器容器】啓動 ssh 服務:

    service ssh start
    
  6. 【本地】使用 VSCODE 連接,添加新的 ssh host 的時候地址這麼寫:

    ssh root@your-server-ip -A -p 5222
    

    這個 5222 就是第 1 步啓動容器的時候設置的映射端口。之後輸入密碼即可,和正常的連接遠程服務器一樣。

完成!
樣例截圖
另外,執行第 5 步之前,可以現在自己本地終端用 ssh 驗證一下是否能連接上,如果出現如下的 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! 錯誤:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
39:e5:ef:60:f1:ae:08:b9:46:36:7c:7d:61:e8:c9:41.
Please contact your system administrator.
Add correct host key in /home/ataliba/.ssh/known_hosts to get rid of this message.
Offending RSA key in /home/ataliba/.ssh/known_hosts:17
RSA host key for host.host.com.br has changed and you have requested strict checking.
Host key verification failed.

則可以在本地執行以下命令重新生成 key 解決:

ssh-keygen -R [your-server-ip]:port

其中 port 爲第 1 步中映射的端口。例如

ssh-keygen -R [192.168.102.1]:5222

然後重新連接即可。

總體來說,在容器內執行的命令如下:

# 設置密碼
passwd root
# 替換國內源
sed -i s:/archive.ubuntu.com:/mirrors.aliyun.com:g /etc/apt/sources.list
sed -i s:/security.ubuntu.com:/mirrors.aliyun.com:g /etc/apt/sources.list
apt update && apt install -y --no-install-recommends openssh-server
sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
service ssh start

至此本文正文就完成了。但是解決我這個需求其實應該還有另外一種官方辦法:Remote - Containers,只是我折騰了半天實在是搞不通,感覺遠沒有剛這個方法簡單快捷。

如果你還想看看我的吐槽,可以往下繼續看,否則,可以關閉這個頁面了。

END

Remote - Containers?

當然首先想到的是用微軟官方出的插件:Remote - Containers,這個屬於遠程開發套件中的一個,主要用於連接容器。其他兩個如下:

  • Remote - SSH:連接遠程服務器,很好用,日常使用
  • Remote - WSL:連接 Windows 的 Linux 子系統,沒怎麼用過

雖然爲遠程開發套件(Remote Development)之一,但是真的能解決我的需求嗎?這要看這個 Remote 怎麼定義了。

默認情況下,這個 Remote 指的是本地的容器,沒錯,是本地的。這是經過我一番折騰後才意識到的。先看下官方文檔是如何定義這個插件的:

The Visual Studio Code Remote - Containers extension lets you use a Docker container as a full-featured development environment. It allows you to open any folder inside (or mounted into) a container and take advantage of Visual Studio Code’s full feature set.

裏面隻字未提 Remote,而提到的時候,是在 Advanced Container Configuration 這一文檔中的 Developing inside a container on a remote Docker host 部分。在這裏的介紹中,我清楚地認識到 Remote - Containers 默認確實指的是連接本地容器(畢竟本地的容器相對於本地來說,也算是遠程操作系統了):

Sometimes you may want to use the Remote - Containers extension to develop inside a container that sits on a remote server. Docker does not support mounting (binding) your local filesystem into a remote container, so VS Code’s default devcontainer.json behavior to use your local source code will not work. While this is the default behavior, in this section we will cover connecting to a remote host so that you can either attach to any running container, or use a local devcontainer.json file as a way to configure, create, and connect to a remote dev container.

看第一句話:Sometimes you may want to use the Remote - Containers extension to develop inside a container that sits on a remote server,Really?真的是 Sometimes 嗎?如此經常的操作沒想到被歸爲「非正常需求」。

然而經過安裝 dockers for Windows,重啓兩次開啓虛擬化,等等等等,半天的一頓折騰後,我放棄了。還要 ssh 免密登陸,還得本地安裝 docker。這真的不如同事說的 pycharm 那樣好用。

REAL END

Reference

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章