Home / Articles

Running Visual Studio Code on Alpine Linux with Docker

2023-01-09T10:00:49.048Z.

This article describes how to run Visual Studio Code on Alpine Linux using Docker.

Note that the approach described in this article shares the X server access to the applications running inside the Docker container. To improve security, consider using x11docker.

Install Docker

Run:


apk add docker

For non-root user to build Docker images and run Docker containers, add the user to the docker group:


# Assume the user is foobar.

addgroup foobar docker

To start the Docker service:


rc-service docker start

Dockerfile and start script

First create the Dockerfile with the following content:


FROM debian:testing-slim

RUN apt update && apt -y upgrade && apt install -y curl gpg

RUN curl --silent --location -o /tmp/vscode.deb \
  'https://code.visualstudio.com/sha/download?build=stable&os=linux-deb-x64'

RUN apt install -y /tmp/vscode.deb && rm /tmp/vscode.deb

COPY ./code.sh /opt/code.sh

ARG user

RUN if [ -z "${user}" ]; then \
      echo 'Missing the "user" build argument.'; \
      false; \
    fi

RUN useradd --create-home "${user}"

USER "${user}"

CMD ["sh", "/opt/code.sh"]

Under the same directory, create the script file code.sh with the following content:


#!/bin/sh

# Run Visual Studio Code.
# The process will exit immediately.
code

# For every 5 seconds, check whether Visual Studio Code is still running.
# If Visual Studio Code is no longer running, quit the loop.
while true
do
  processes=$(ls -l /proc/*/exe)
  count=$(echo "${processes}" | grep -c /usr/share/code/code)
  if [ "${count}" -eq 0 ]; then
    break
  fi
  sleep 5
done

When building the Docker image, it basically:

Visual Studio Code is not run directly using the last CMD instruction because the code program will exit immediately. Once the last instruction is completed, Docker container will stop, so Visual Studio Code running within the Docker container will also be stopped. To keep the Docker container running, a script is executed instead. The script code.sh will first start Visual Studio Code, and for every 5 seconds, check whether Visual Studio Code is still running. Once Visual Studio Code is no longer running, the periodical check will end and the script will exit, finally the Docker container will stop.

Build the Docker image

To build the Docker image:


# Under the same directory as the Dockerfile.
docker build -t vscode --build-arg user=$(whoami) .

A Docker image with the tag vscode should be built. Note that the name of the current user is passed via the user argument, so a user with the same name as the current user will be created within the Docker image.

Prepare required directories

Visual Studio Code requires write permission of several directories when running the application. Run the following to prepare them:


mkdir -p ~/.config/Code
mkdir -p ~/.vscode

Run a Docker container

To run a Docker container using the Docker image built above:


docker run -d \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  -v ~/.Xauthority:/home/$(whoami)/.Xauthority \
  -v ~/.config/Code:/home/$(whoami)/.config/Code \
  -v ~/.vscode:/home/$(whoami)/.vscode \
  -v ~/Projects:/home/$(whoami)/Projects \
  -w /home/$(whoami) \
  -e DISPLAY=$DISPLAY \
  --rm \
  --network=host \
  --cap-add=SYS_ADMIN \
  --security-opt=no-new-privileges \
  vscode

Explanation of some of the options:

References