有没有办法在
Dockerfile
中安装 VS Code 扩展?
显然,虽然大多数基于浏览器的 VS Code 分支(包括
openvscode-server
)不允许无头安装 VS Code 扩展(从我的other answer 可以看出),可以使用 docker build
在其中之一:代码服务器(代码服务器),就像在这个示例中Dockerfile
:
FROM ubuntu:22.04
RUN apt update && apt install -y curl
# install VS Code (code-server)
RUN curl -fsSL https://code-server.dev/install.sh | sh
# install VS Code extensions
RUN code-server --install-extension redhat.vscode-yaml \
--install-extension ms-python.python
docker build
日志的相关片段:
[..]
---> Running in 59eea050a2db
[2022-11-13T10:13:58.762Z] info Wrote default config file to ~/.config/code-server/config.yaml
Installing extensions...
Installing extension 'redhat.vscode-yaml'...
Installing extension 'ms-python.python'...
Extension 'redhat.vscode-yaml' v1.10.1 was successfully installed.
Extension 'ms-python.python' v2022.16.1 was successfully installed.
[..]
根据 VS 代码文档,扩展属性特定于 VS 代码,只能使用 .devcontainer 进行配置。
你能做的最好的事情是如果扩展有 CLI,你可以安装它。例如,
RUN npm install prettier -D --save-exact
然后使用npx:
npx prettier --check .
遗憾的是,这是设计不允许的,当您尝试运行
docker build
或code --install-extension
:时,您将在
openvscode-server --install-extension
日志中看到此错误消息所证实
Command is only available in WSL or inside a Visual Studio Code terminal.
在这个
GitHub 问题中,一位 VS Code Remote 开发人员也确认(并标记)为
as designed
:
这是正确的,'vs 代码服务器 CLI' 只能从集成终端使用。
所以 VS Code Remote 甚至
openvscode-server
都不可能为这个流行的 Microsoft IDE 自动安装第一方或第三方扩展,除非你在他们的闭源 IDE 中运行他们的自定义终端,这通常需要购买许可证他们的基于 GUI 的闭源操作系统;)
如果您的目标不是重复安装 VS 代码扩展,我的建议是挂载 $HOME/.vscode-server/.
例如,在一个 docker-compose.yml
services:
your_container:
...
volumes:
- ./volume/vscode-server:$HOME/.vscode-server
或者在
docker run
docker run -it -v ./volume/vscode-server:$HOME/.vscode-server your_image bash
然后,在容器内安装所需的扩展。下次设置容器时,无需重新安装扩展程序。