小智 6

如果您的目标不是重复安装 VS code 扩展,我的建议是挂载 $HOME/.vscode-server/。

例如,在 docker-compose.yml 中

services:
    your_container:
        ...
        volumes:
            - ./volume/vscode-server:$HOME/.vscode-server
Run Code Online (Sandbox Code Playgroud)

或者在docker run

docker run -it -v ./volume/vscode-server:$HOME/.vscode-server your_image bash
Run Code Online (Sandbox Code Playgroud)

然后,在容器内安装所需的扩展。下次设置容器时,无需重新安装扩展。


mir*_*phd 5

显然,虽然大多数基于浏览器的 VS Code 分支(包括openvscode-server)不允许无头安装 VS Code 扩展(从我的其他答案中可以看出),但可以使用docker build其中之一进行此类自动安装:Code Server(代码-服务器,如本示例所示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
Run Code Online (Sandbox Code Playgroud)

对于较长的扩展列表,可以使用简单的循环按顺序执行安装:

RUN EXT_LIST="redhat.vscode-yaml ms-python.python" && \
    for EXT in $EXT_LIST; do code-server --install-extension $EXT; done
Run Code Online (Sandbox Code Playgroud)

日志的相关部分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.
[..]
Run Code Online (Sandbox Code Playgroud)


cwa*_*ort 0

根据VS Code 文档,扩展属性特定于 VS Code,只能使用 .devcontainer 进行配置。

你能做的最好的事情就是如果扩展有 CLI,你就可以安装它。例如,

RUN npm install prettier -D --save-exact
Run Code Online (Sandbox Code Playgroud)

然后使用 npx:

npx prettier --check .
Run Code Online (Sandbox Code Playgroud)