利用 VS Code 構建基於容器的開發環境

作者 | Santhosh Sundar       

譯者 | 彎月    責編 | 歐陽姝黎

出品 | CSDN(ID:CSDNnews)

長話短說,你可以使用 Docker 和 VS Code 的遠程容器來建立一個容器化的本地開發環境,這樣就可以讓團隊成員儘快完成入門培訓。你不僅可以在所有環境中使用同一個基礎鏡像,而且還可以爲所有開發人員提供相同的編輯器,此外還更容易標準化實現。但這種方法並非適合所有人,如果你不喜歡 VS Code 作爲代碼編輯器的話,則可以跳過本文,除非你想嘗試一下。

爲團隊設置本地開發環境時,我們所面臨的挑戰之一就在於,確保所有開發人員的設置都相同或者能夠滿足需求。這個問題常見的解決方法是制定入門指南,並希望開發人員遵循這些指南。但是,由於版本兼容性問題和個人經驗等問題,導致我們無法使用正確的工具,因此實現統一的環境設置實則困難重重。

圖:常見的環境設置方法。

另外一種解決方案是,準備一個預先配置好的開發環境,其中包含了所有必需的庫及依賴項,該環境可以直接作爲容器啓動。這樣,開發人員就可以在容器提供的隔離環境中工作了。這種方式可以極大地減少開發人員花費在克隆代碼庫上的時間。

圖:使用基於容器的開發環境。

除了爲所有開發人員提供相同的環境之外,我們還可以在 Visual Studio Code 中使用同一套工具、擴展甚至主題集。儘管這不是必須的,但我們可以利用它來自動安裝項目所需的特定擴展。這種方式可以避免工具的不一致,而且開發人員也可以免卻手動安裝的麻煩。

所有這些工作都可以通過 Docker 與 VS Code 的 Remote-Containers 擴展的結合來實現。

設置

在本文中,我將展示一個在 Node 環境中運行 JavaScript 應用程序的示例。有關的詳細說明請參見官方文檔(https://code.visualstudio.com/docs/remote/containers)。

# Specify the base image you want your dev container to use.
# You may use the same exact base image your application would use in production for consistancy.
# That could prevent surprises such as "works in local, but not in PROD".
FROM node:14.17.0-alpine
# Additionally you can install other dependencies for the environment while configuring the base image.
# In this example, I am installing Git as the Alpine version of node does not come with one.
RUN apk update
RUN apk add git
{
"name": "DevContainer ReactApp",
// Provide the dev container with a Dockerfile that it can use to build an image and run the container.
"dockerFile": "Dockerfile",
// Command(s) to run before the container is created.
// In this case we are installing the node modules.
"initializeCommand": "yarn install",
// Starts the development server every time the container starts.
// This is triggered on reopening the container as well.
"postStartCommand": "yarn start",
// Forward your application's port(s) running in the container to the local machine.
"forwardPorts": [3000],
// Required VSC code extensions that you want to automatically install for the developers to use.
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"eamodio.gitlens"
]
// Use the devcontainer.json reference to explore all possible configurations.
// https://code.visualstudio.com/docs/remote/devcontainerjson-reference
}

在完成上述工作後,我們來構建容器。首先,點擊 VS Code 命令面板中的 “Open Folder in Container” 或“Reopen in Container”。

這一步是初始化開發容器,拉取 Docker 基礎鏡像、配置容器,然後啓動開發服務器。

完成這一步,你就應該能夠在瀏覽器中訪問應用程序,並正常使用 VS Code 進行開發了。就連熱重載都能正常工作!我創建了一個代碼庫(https://github.com/Gigacore/devcontainer-react-example),其中包含一個示例,你可以嘗試一下!

容器的構建和配置只需要執行一次,但是需要一定的時間。以後如果不發生變化,那麼重建會更快。但是,如果 devcontainer.json 或 Dockerfile 發生變化,則需要重新構建。如果你嘗試直接重新打開,系統會提示你重建。

在退出容器或 VS Code 後,下一次可以通過 ”Reopen in Container” 選項重新進入容器。該選項會啓動已配置的容器,並再次啓動開發服務器。如果 VS Code 在代碼庫中找到 .devcontainer 配置,則會自動提示你啓動容器。

常見問題和解決方法

並非適合所有人

總結

這是一個相對較新的概念,有許多地方需要探索,而且也有很多限制需要解決。我個人很喜歡這種方式,而且也比較推薦。如果你也採用了這種方式,請在下方留言分享你的經驗。

原文鏈接:https://santhoshsundar.medium.com/building-container-based-development-environment-with-visual-studio-code-2d7111c650bd

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/SG8_lO_vjOgmXdUbRJ7N8Q