Code-server: Installing extensions from command line

Created on 9 Mar 2019  路  19Comments  路  Source: cdr/code-server

It would be great to be able to script the installation of extensions,
e.g. after starting code-server in a new container.

A feature similar to VScode's code --install-extension would be very helpful.
See the example here.

Or am I missing another way to install extensions to code-server from the command line?

enhancement

Most helpful comment

@ThorbenJensen I think you can do this by just extending the Dockerfile as @jeasonstudio suggests.

For example, the following installs the Preview extension. I needed it because I couldn't get the built-in Preview functions to work out-of-the-box and also it comes with Mermaid diagram support, which the built-in previewer does not.

RUN apt-get update && apt install -y bsdtar curl
RUN mkdir -p /root/.local/share/code-server/extensions
RUN curl -JL https://marketplace.visualstudio.com/_apis/public/gallery/publishers/searKing/vsextensions/preview-vscode/2.0.3/vspackage | bsdtar -xvf - extension
RUN mv extension /root/.local/share/code-server/extensions/searKing.preview-vscode-2.0.3

The lines above do the following.

  1. Install bsdtar and curl. We install bsdtar because gnutar, which ships with Ubuntu and Debian, doesn't function properly with the archives we get from the Visual Studio web site.
  2. Create an extensions subdirectory for the code-server data directory. The data directory path above (/root/.local/share/code-server/) is the default from what I can tell. You can look at the logs to verify that it's the same with your configuration. You'll see something like INFO Initializing {"data-dir":"/root/.local/share/code-server","working-dir": ...,"log-dir": ...} when starting the container.
  3. Download and extract the extension file from the Visual Studio web site. The URL follows the form https://marketplace.visualstudio.com/_apis/public/gallery/publishers/<PUBLISHER>/vsextensions/<EXTENSION-NAME>/<VERSION>/vspackage. We only extract the extension folder.
  4. Move the extension folder to a properly named folder of the form <PUBLISHER>.<EXTENSION-NAME>-<VERSION> in the extensions subdirectory.

If all goes well, you should be able to get the following.

Screen Shot 2019-03-17 at 1 05 41 PM

Hope this helps!

All 19 comments

@ThorbenJensen have you tried using ext install <package name>?

@sr229
thank you for this idea - I have just tested it.
I have the feeling that ext install <package name> does not work outside the GUI/browser?
If I am mistaken, please let me know.

I would like to be able to install extensions from the command line - before accessing code-server in the browser.
I am looking for something like the VSCode feature of working with extensions from the command line.
How it is done in VSCode is also documented here.

@ThorbenJensen I guess you want to pre-load some extensions before running.
yarn task build:copy-vscode in build/tasks.ts#171 may help.

@jeasonstudio I think this goes into the right direction, thank you.
Unfortunately, my javascript is not good enough to adapt build/tasks to my needs.

Is there anything I can do (e.g. yarn ... ) to install an extension after the pre-built container is running?
I would like to enter the running container from codercom/code-server, and run a command for installation of a package.

@ThorbenJensen For now, you can just copy extension folder to $HOME/.code-server/extensions. It will work.

cp -r $HOME/.vscode-insiders/extensions/spywhere.guides-0.9.3 $HOME/.code-server/extensions/spywhere.guides-0.9.3

@jeasonstudio Thank you for the hint.
I suppose this is not an option for my situation, considering that I just want to pull and start the container.
At maximum, I would extend the Dockerfile.

Nonetheless, I will check out code-server again in a few months.
Very impressive and useful so far!

As far as I am concerned, this issue can be closed.
Of course you can leave it open, if you feel like implementing this feature down the road.

@ThorbenJensen I think you can do this by just extending the Dockerfile as @jeasonstudio suggests.

For example, the following installs the Preview extension. I needed it because I couldn't get the built-in Preview functions to work out-of-the-box and also it comes with Mermaid diagram support, which the built-in previewer does not.

RUN apt-get update && apt install -y bsdtar curl
RUN mkdir -p /root/.local/share/code-server/extensions
RUN curl -JL https://marketplace.visualstudio.com/_apis/public/gallery/publishers/searKing/vsextensions/preview-vscode/2.0.3/vspackage | bsdtar -xvf - extension
RUN mv extension /root/.local/share/code-server/extensions/searKing.preview-vscode-2.0.3

The lines above do the following.

  1. Install bsdtar and curl. We install bsdtar because gnutar, which ships with Ubuntu and Debian, doesn't function properly with the archives we get from the Visual Studio web site.
  2. Create an extensions subdirectory for the code-server data directory. The data directory path above (/root/.local/share/code-server/) is the default from what I can tell. You can look at the logs to verify that it's the same with your configuration. You'll see something like INFO Initializing {"data-dir":"/root/.local/share/code-server","working-dir": ...,"log-dir": ...} when starting the container.
  3. Download and extract the extension file from the Visual Studio web site. The URL follows the form https://marketplace.visualstudio.com/_apis/public/gallery/publishers/<PUBLISHER>/vsextensions/<EXTENSION-NAME>/<VERSION>/vspackage. We only extract the extension folder.
  4. Move the extension folder to a properly named folder of the form <PUBLISHER>.<EXTENSION-NAME>-<VERSION> in the extensions subdirectory.

If all goes well, you should be able to get the following.

Screen Shot 2019-03-17 at 1 05 41 PM

Hope this helps!

A lightweight alternative could be to use workspace recommended extensions

A lightweight alternative could be to use workspace recommended extensions

This is currently not supported. What needs to be done to enable workspace extensions team?

@faustomorales great ideas, thanks a lot!
It appears that the extension folder in the container I extended from codercom/code-server is located at /root/.code-server/extensions.

So, heavily inspired by what you suggested, this is the Dockerfile that works for me:

FROM codercom/code-server
RUN apt-get update && apt-get -y upgrade

# VSCode extensions
RUN apt-get install -y bsdtar curl
RUN mkdir -p /root/.code-server/extensions
RUN curl -JL https://github.com/Microsoft/vscode-python/releases/download/2019.2.5558/ms-python-release.vsix | bsdtar -xvf - extension
RUN mv extension /root/.code-server/extensions/ms-python.python-vscode-2.0.3

CMD ["code-server", "--allow-http", "--no-auth"]

If anyone is interested in testing this Dockerfile:

$ sudo docker build . -t code-server-test
$ sudo docker run -p 8443:8443 -v "${PWD}:/root/project" code-server-test

Thanks, again :-)

@faustomorales great ideas, thanks a lot!
It appears that the extension folder in the container I extended from codercom/code-server is located at /root/.code-server/extensions.

So, heavily inspired by what you suggested, this is the Dockerfile that works for me:

FROM codercom/code-server
RUN apt-get update && apt-get -y upgrade

# VSCode extensions
RUN apt-get install -y bsdtar curl
RUN mkdir -p /root/.code-server/extensions
RUN curl -JL https://github.com/Microsoft/vscode-python/releases/download/2019.2.5558/ms-python-release.vsix | bsdtar -xvf - extension
RUN mv extension /root/.code-server/extensions/ms-python.python-vscode-2.0.3

CMD ["code-server", "--allow-http", "--no-auth"]

If anyone is interested in testing this Dockerfile:

$ sudo docker build . -t code-server-test
$ sudo docker run -p 8443:8443 -v "${PWD}:/root/project" code-server-test

Thanks, again :-)

@ThorbenJensen

I tried your dockerfile above and it fails to build and here is the output from the build.
I am getting permission denied. I want to install extension and could you please provide any reference to doc or Dockerfile which works.?

docker build . -t code-server-test
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM codercom/code-server
 ---> 9ea98ff48a65
Step 2/7 : RUN apt-get update && apt-get -y upgrade
 ---> Running in c57c1d8273f9
Reading package lists...
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
The command '/bin/sh -c apt-get update && apt-get -y upgrade' returned a non-zero code: 100

@jagraj The recommended way to install extensions now is to use code-server --install-extension <value>. You can give it the path to a .vsix file or the ID (looks like organization.extension-name).

@jagrag I can confirm that code-server --install-extension <value> is the new install mechanism that works. You see an example in the Dockerfile here:
https://github.com/ThorbenJensen/code-server-blueprint/blob/master/Dockerfile

@jagraj The recommended way to install extensions now is to use code-server --install-extension <value>. You can give it the path to a .vsix file or the ID (looks like organization.extension-name).

Note that while it does work, it imposes a root requirement on the containerized system, iow VS Code now works only for root-owned containers. Trying to run it as non-root (e.g. docker run -u 1001) causes blank screen to be displayed in the browser instead of VS Code (which now works only for docker run -u 0 ). If you insert code-server --install-extension code in the root-owned part of the Dockerfile (before switch to non-root user, e.g. via USER coder), the extension would not be installed (would not be visible when running the container as a non-root user, here: _coder_ with id 1001).

RUN mkdir -p /root/.local/share/code-server/extensions

@faustomorales , I think the default location for VS extensions can be made more general to allow for non-root containers, thus:

USER username
RUN mkdir -p ~/.local/share/code-server/extensions

@deansheather , hard-coding root folder in the default extensions location may be the reason why the new code-server --install-extension option works only for containers run with root UID.

_More info_
In containers run under non-root users (required e.g. in Openshift) extensions are either not available to such users or VS Code fails to appear, displaying blank screen, depending on who installed the extension in the Dockerfile, root or standard user. More precisely, calling RUN code-server --install-extension in your Dockerfile:

  • imposes root requirement if it follows USER coder in the Dockerfile (-u 10001 switch no longer works, you need to use the default -u 0 to see the VS Code web interface) or
  • fails to install the extension (for non-root users) if it precedes USER coder in the Dockerfile.

Thanks @faustomorales!

above solutions are not working? any more solutions? @faustomorales @ThorbenJensen @coadler

@sanjayme97 I needed a container for python data science running on EC2 with code-server up and running. This is my custom dockerfile which builds successfully

FROM python:3.7.7-slim

WORKDIR /home

RUN /bin/bash -c "apt-get update -y;apt-get install wget -y"

RUN wget https://github.com/cdr/code-server/releases/download/3.2.0/code-server-3.2.0-linux-x86_64.tar.gz
RUN tar -xzvf code-server-3.2.0-linux-x86_64.tar.gz

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

ENV PASSWORD=demo

CMD /bin/bash -c "./code-server-3.2.0-linux-x86_64/code-server --install-extension ms-python.python --force --install-extension ms-azuretools.vscode-docker --force && ./code-server-3.2.0-linux-x86_64/code-server --host 0.0.0.0 --port 8989" 

See if this works. extensions can be separated to a single run command too. But I have put this up hastly and haven't tested that approach

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avelino picture avelino  路  3Comments

RealSlimMahdi picture RealSlimMahdi  路  3Comments

korzq picture korzq  路  3Comments

nol166 picture nol166  路  3Comments

justmao945 picture justmao945  路  3Comments