16

Is there a way to install VS Code extensions in a Dockerfile?

mirekphd
  • 4,799
  • 3
  • 38
  • 59
curtank
  • 600
  • 2
  • 7
  • 18

4 Answers4

5

If your goal is not to repeat the installation of the VS code extensions, my suggestion is to mount $HOME/.vscode-server/.

For example, in a docker-compose.yml

services:
    your_container:
        ...
        volumes:
            - ./volume/vscode-server:$HOME/.vscode-server

Or in docker run

docker run -it -v ./volume/vscode-server:$HOME/.vscode-server your_image bash

Then, install the required extensions inside the container. The next time you set up the container, there will be no need to reinstall extensions.

Marc-Antoine
  • 51
  • 1
  • 1
  • Doesn't work fully for me. Extensions are installed, but eg. cpptool can't start: `cpptools client: couldn't create connection to server.... failed. .../cpptools EACCES`. If mounted it doesn't seem, to be able to fix the file permissions. Doing it all the time by hand would be a bit tedious. – BadAtLaTeX Jun 06 '23 at 18:05
3

Apparently, while most browser-based VS Code forks (including openvscode-server) do not permit headless installation of VS Code extensions (as seen from my other answer), it is possible to do such automated installs using docker build in one of them: Code Server (code-server), like in this sample 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

For longer extensions lists the installation can be performed sequentially using a simple loop:

RUN EXT_LIST="redhat.vscode-yaml ms-python.python" && \
    for EXT in $EXT_LIST; do code-server --install-extension $EXT; done

Relevant part of the docker build log:

[..]
 ---> 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.
[..]
mirekphd
  • 4,799
  • 3
  • 38
  • 59
0

Per the VS Code Documentation, the extensions property is specific to VS Code and can only be configured using .devcontainer.

The best you can do is if the extension has a CLI, you can install that. For example,

RUN npm install prettier -D --save-exact

Then use npx:

npx prettier --check .
cwalvoort
  • 1,851
  • 1
  • 18
  • 19
0

This is sadly disallowed by design, as confirmed by this error message you will see in your docker build log when you attempt to run code --install-extension or openvscode-server --install-extension:

Command is only available in WSL or inside a Visual Studio Code terminal.

This is also confirmed (and tagged) as being as designed by one of VS Code Remote devs in this GitHub issue:

This is correct the 'vs code server CLI' is only available from the integrated terminal.


So VS Code Remote or even openvscode-server make it impossible to automate installs of first- or third-party extensions for this popular Microsoft IDE, unless you run their custom terminal inside their closed-source IDE, which normally entails buying a license for their GUI-based closed-source operating system as well;)

mirekphd
  • 4,799
  • 3
  • 38
  • 59