1. dev container
docker和容器化技術(shù)讓運(yùn)維有了質(zhì)的飛躍,從此,部署軟件再也無需擔(dān)心軟件運(yùn)行所需的繁雜環(huán)境,只要拉取鏡像然后運(yùn)行就可以將應(yīng)用連帶其部署的環(huán)境一步到位。
但是回想起我們的開發(fā)過程,誰還不是依然需要先安裝一堆環(huán)境(編譯器、庫等等),然后才能進(jìn)行開發(fā)。如果我們恰好還需要開發(fā)各種語言、各種應(yīng)用,那光是繁雜的環(huán)境就足以把電腦搞的亂糟糟。更何況,大多數(shù)語言會(huì)擁有很多版本,而且版本之間兼容性不佳(例如Python),在自己電腦上安裝多個(gè)版本的python有時(shí)候已經(jīng)讓人血壓升高,何況我還要記得哪個(gè)版本在哪,哪個(gè)版本的庫在哪,不同的版本用的是哪一個(gè)包管理器等等繁瑣的細(xì)節(jié)……
于是,dev container的想法就產(chǎn)生了。顧名思義,就是在容器中開發(fā)。這樣一來,開發(fā)的應(yīng)用連帶這開發(fā)環(huán)境就被”連根拔起“,以后就再也不需要擔(dān)心重新配置開發(fā)環(huán)境的麻煩了,要做的僅僅是確認(rèn)一下機(jī)器上有沒有docker,然后運(yùn)行一個(gè)容器開發(fā)就可以。
下面就以python為例,來說明一下如何使用vscode進(jìn)行容器化開發(fā)。vscode中的遠(yuǎn)程開發(fā)和容器開發(fā)插件讓dev container從概念變成了現(xiàn)實(shí)。
2. 安裝插件
我不想將容器運(yùn)行在本地,于是采用了先遠(yuǎn)程開發(fā)連接服務(wù)器,再使用服務(wù)器上的容器開發(fā)的方式。但是不管怎樣,本地vscode上安裝了插件才能和遠(yuǎn)程連接開發(fā)。
要安裝的核心插件是ms-vscode-remote.remote-ssh和ms-vscode-remote.remote-containers兩個(gè)。如圖所示:
安裝之后,可以在左側(cè)看到遠(yuǎn)程連接的選項(xiàng)卡,進(jìn)入后如圖所示:
上面的下拉框可以切換是主機(jī)還是container。我們要做的是先ssh連接一臺(tái)主機(jī),再打開這個(gè)選項(xiàng)卡選到containers,就可以管理主機(jī)上的容器了。
3.配置文件
容器化開發(fā)的容器配置應(yīng)該是因項(xiàng)目而異的。因?yàn)閷?duì)于每個(gè)項(xiàng)目,所需要的環(huán)境都不盡相同。就算是兩個(gè)python項(xiàng)目,開發(fā)中用到的包也不一樣,不應(yīng)該使用一個(gè)容器開發(fā)多個(gè)項(xiàng)目。因此,項(xiàng)目的容器配置會(huì)放在項(xiàng)目目錄下。vscode已經(jīng)為我們準(zhǔn)備好了范式,我們可以在項(xiàng)目下使用.devcontainer目錄,存放devcontainer.json和Dockerfile、docker-compose.yaml文件。
devcontainer.json是vscode對(duì)容器的配置文件,里面存放了要啟動(dòng)或構(gòu)建的鏡像、要附帶安裝的vscode插件、創(chuàng)建后執(zhí)行的命令等一系列信息。Dockerfile中描述了我們特定項(xiàng)目需要的容器應(yīng)該怎么構(gòu)建。雖然可以直接使用一個(gè)官方鏡像開發(fā),但是最好還是做一些定制化,構(gòu)建項(xiàng)目個(gè)性化的鏡像。docker-compose.yaml中指定了鏡像應(yīng)該如何運(yùn)行。
以下就是python項(xiàng)目中對(duì)應(yīng)配置文件的示例:devcontainer.json:
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.202.3/containers/python-3
{
"name": "Python 3",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
// Update 'VARIANT' to pick a Python version: 3, 3.9, 3.8, 3.7, 3.6.
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"VARIANT": "3.9",
// Options
"NODE_VERSION": "lts/*"
}
},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.profiles.linux": {
"bash": {
"path": "/bin/bash"
}
},
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.languageServer": "Default",
"python.linting.enabled": false,
"python.linting.pylintEnabled": false,
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"formulahendry.code-runner"
// "ms-python.vscode-pylance"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [9000],
// Use 'portsAttributes' to set default properties for specific forwarded ports. More info: https://code.visualstudio.com/docs/remote/devcontainerjson-reference.
// "portsAttributes": {
// "9000": {
// "label": "Hello Remote World",
// "onAutoForward": "notify"
// }
// },
// Use 'otherPortsAttributes' to configure any ports that aren't configured using 'portsAttributes'.
// "otherPortsAttributes": {
// "onAutoForward": "silent"
// },
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install -r requirements.txt",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "root",
// claws: use this option to run as root in container, else you can not save file because the project is in /root
"containerUser": "root"
}
幾個(gè)需要說明的地方:
- build.args中指定了一些構(gòu)建鏡像時(shí)帶入的參數(shù),會(huì)影響具體應(yīng)用的構(gòu)建。
- customization.vscode.extensions中指定了容器中要安裝的插件,vscode創(chuàng)建開發(fā)容器時(shí)會(huì)自動(dòng)安裝這些插件。
- postCreateCommand中指定了容器創(chuàng)建成功后要執(zhí)行的操作,通常可以在這里安裝依賴。
- containerUser指定了容器運(yùn)行的時(shí)候采用什么用戶,默認(rèn)是vscode,這里改成root,避免項(xiàng)目放在宿主機(jī)的/root下導(dǎo)致容器中修改無權(quán)限的情況。(因?yàn)槿萜鬟\(yùn)行時(shí),項(xiàng)目目錄是掛載到宿主機(jī)的)
- remoteUser指定了連接到容器使用的用戶名,暫時(shí)還沒搞清用途。。
Dockerfile:
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/blob/v0.202.3/containers/python-3/.devcontainer/base.Dockerfile
# [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster
ARG VARIANT=3.9
FROM mcr.microsoft.com/devcontainers/python:0-${VARIANT}
# [Optional] Allow the vscode user to pip install globally w/o sudo
# ENV PIP_TARGET=/usr/local/pip-global
# ENV PYTHONPATH=${PIP_TARGET}:${PYTHONPATH}
# ENV PATH=${PIP_TARGET}/bin:${PATH}
# RUN if ! cat /etc/group | grep -e "^pip-global:" > /dev/null 2>&1; then groupadd -r pip-global; fi \
# && usermod -a -G pip-global vscode \
# && umask 0002 && mkdir -p ${PIP_TARGET} \
# && chown :pip-global ${PIP_TARGET} \
# && ( [ ! -f "/etc/profile.d/00-restore-env.sh" ] || sed -i -e "s/export PATH=/export PATH=\/usr\/local\/pip-global:/" /etc/profile.d/00-restore-env.sh )
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
# ARG NODE_VERSION="none"
# RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
COPY requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
&& rm -rf /tmp/pip-tmp
# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends git
# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
簡(jiǎn)單說明:
- 開頭指定了一個(gè)arg,和上面devcontainer.json文件中的對(duì)應(yīng),聲明的參數(shù)值會(huì)在創(chuàng)建容器的時(shí)候被devcontainer.json文件中的參數(shù)值替換。
- 后面可以規(guī)定一些依賴進(jìn)行安裝,也可以安裝一些額外的軟件包。盡量使用這份示例中的參數(shù),避免生成緩存,增大鏡像的大小。
4.使用技巧
安裝遠(yuǎn)程插件后,vscode打開項(xiàng)目目錄時(shí)會(huì)自動(dòng)循環(huán)是否啟動(dòng)container后重新打開,選是就可以快速構(gòu)建啟動(dòng)容器,然后再容器中打開掛載的項(xiàng)目目錄進(jìn)行開發(fā)。如果后來對(duì)容器配置進(jìn)行了修改,可以用Ctrl+Shift+p快速打開命令面板,輸入dev container rebuild,選擇搜索出來的選項(xiàng)進(jìn)行重新構(gòu)建打開:文章來源:http://www.zghlxwxcb.cn/news/detail-826625.html
啟動(dòng)的開發(fā)容器會(huì)自動(dòng)運(yùn)行在docker后臺(tái),在remote插件中可以對(duì)容器進(jìn)行管理,包括start、stop、remove等,而且狀態(tài)一目了然,非常方便。文章來源地址http://www.zghlxwxcb.cn/news/detail-826625.html
到了這里,關(guān)于[Docker] DevContainer高效開發(fā)(第一篇):基于remote container開發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!