隨時隨地敲代碼,基於Theia快速部署自己的雲開發環境


如果手頭上有多個設備,需要經常輪換着使用,又或者經常出門不想帶太沉的筆記本電腦,想隨時隨地寫代碼,Web IDE 可以幫到你。

Web IDE,顧名思義就是雲端開發環境,把 IDE 部署在雲上。打開瀏覽器,連上雲 IDE,就可以方便敲代碼跑程序。想象一下,帶上一台iPad或者華為、小米平板,配上藍牙鍵盤,坐在陰涼的動物園樹下,邊看動物邊敲代碼,多愜意。

其實 Web IDE 很早就有,但都只能說是玩具,直到現在才有一些不錯的產品推出,比如微軟和 Github 的Visual Studio Codespaces、coding.net 的 Cloud Studio、華為雲 CloudIDE 等。不過,這些產品要么還在測試,要么免費用會有限制或者價格不低,目前還不是非常方便。

如果想要低成本愉快使用,自己來搭建一個是不錯的方案。對配置要求不高的話,一年幾十塊一百多塊就能買到廉價的 VPS 或者雲主機。Web IDE 的部署方案推薦兩個,code-server 和 Theia。

Code-server(https://github.com/cdr/code-server)是由 Coder 開發的,把 VS Code 搬到了瀏覽器上。

Theia(https://theia-ide.org)是 Eclipse 推出的雲端和桌面 IDE 平台,完全開源。Theia 是基於 VS Code 開發的,它的模塊化特性非常適合二次開發,比如華為雲 CloudIDE、阿里雲 Function Compute IDE 便是基於 Theia 開發。

下面就以Theia為例,使用 Docker 部署在一台 4核 8GB 內存 60 GB SSD 的 VPS上。這台 VPS 一年 24 刀,配置能滿足基本的使用。這篇文章很長,但部署過程很簡單,如果不想看太多字,可直接拉到最后。

安裝Docker

如果沒有安裝 Docker,先安裝一下。我使用的是 CentOS 7:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
sudo service docker start
sudo systemctl enable docker

也可以用腳本自動安裝,這種方法比較方便,而且適合在不同系統中安裝,但官方不建議在生產環境使用:

curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh
# 如果連接速度慢,可加上--mirror參數指定鏡像,如 sudo sh get-docker.sh --mirror Aliyun

為什么不直接部署標准版鏡像

Theia 提供了不同版本的鏡像,可以在 https://github.com/theia-ide/theia-apps 選擇自己需要的語言版本,可以支持 C++ / Go / Python / Java / PHP / Ruby 等多種語言。最簡單的方法,就是直接獲取鏡像啟動容器。

比如拉取完整版鏡像並在當前目錄運行:

docker pull theiaide/theia-full
docker run -it --init -p 3000:3000 -v "$(pwd):/home/project:cached" theiaide/theia-full:latest

其中,$(pwd) 代表的是將當前目錄掛載到 Docker 容器中,也可以指定文件目錄。

打開 http://localhost:3000 就可以使用。

然而,需要特別注意的是,Theia 本身沒有認證機制,任何知道公網 IP 和端口號的人都可使用。因此,不推薦這種方法。

構建更安全的版本

Theia-https-docker 增加了 token 認證和 https,可以在標准鏡像中加入 security layer,強烈建議使用它構造自己的鏡像。構建也非常簡單,按以下三個步驟操作即可,其中第三步的 --build-arg app= 填入需要使用的語言版本,這里使用的也是 full 版本。

git clone https://github.com/theia-ide/theia-apps.git
cd theia-apps/theia-https-docker
docker build . --build-arg app=theia-full -t theiaide/theia-full-sec

耐心等到構建完成,輸入docke images就可看到自己構建的 theiaide/theia-full-sec 鏡像。

之后就可運行,token 后接的是訪問口令:

docker run --init -it -p 10443:10443 -e token=mysecrettoken -v "$(pwd):/home/project:cached" theiaide/theia-full-sec

但一般我們都需要后台運行,可以這樣讓容器后台運行:

docker run --init -itd -p 10443:10443 -e token=mysecrettoken -v "$(pwd):/home/project:cached" theiaide/theia-full-sec

如果要指定使用 /home/coding目錄,后台運行,則是:

docker run --init -itd -p 10443:10443 -e token=mysecrettoken -v "$(pwd):/home/project:cached" theiaide/theia-full-sec

打開 https://ip地址:10443,輸入 token 便可打開 Web IDE。也可直接使用 https://ip地址:10443/?token=mysecrettoken 直接打開。

解決權限問題

然而,如果這時使用,會發現 Theia 無法寫入文件。這是 Theia 默認使用了 1000 的 userid,跟宿主機不一樣,從而造成的權限問題。

解決方法有這么幾個:

  1. 將掛載的文件權限改為 777,這種方法不太安全:
    chmod -R 777 /home/coding
  2. 指定用戶運行,但如果使用的是 root,仍會有些不安全:
    docker run --user=root --init -it -p 10443:10443 -e token=mysecrettoken -v "$(pwd):/home/project:cached" theiaide/theia-full-sec
  3. 將掛載的文件夾屬主改為1000,推薦這種方法:
    chown -R 1000 /home/coding

這樣,就可以愉快使用了:

部署標准版並反代

如果覺得 theia-https-docker 不太好用,或者有更多的需求,也可以部署標准版的鏡像,然后用 Nginx 反代並且綁定域名。具體操作可以參考 https://www.digitalocean.com/community/tutorials/how-to-set-up-the-eclipse-theia-cloud-ide-platform-on-ubuntu-18-04 這篇教程。

總結

curl -fsSL get.docker.com -o get-docker.sh

sudo sh get-docker.sh

git clone https://github.com/theia-ide/theia-apps.git

cd theia-apps/theia-https-docker

docker build . --build-arg app=theia-full -t theiaide/theia-full-sec

docker run --init -itd -p 10443:10443 -e token=mysecrettoken -v "$(pwd):/home/project:cached" theiaide/theia-full-sec

chown -R 1000 /home/coding`

參考

[1] https://github.com/theia-ide/theia-apps
[2] https://www.cnblogs.com/sparkdev/p/9614164.html
[3] https://gist.github.com/heyfluke/b8372df866ec2584f9a51ca7d7fe9ebb


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM