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:
- Using Debian (testing) as the base image.
- Update the system and install necessary packages for installing Visual Studio Code.
- Download the Visual Studio Code package and install it.
- Copy the script file
code.sh
to the Docker image. -
Check that if the
user
argument is set, if not then the build process will stop. -
Within the Docker image, create a user using the username specified via
the
user
argument. -
On behalf of the created user, run the script file
code.sh
.
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:
-
-v /tmp/.X11-unix:/tmp/.X11-unix
: Within the Docker container, the programs have to talk to the X server to show the program on screen and by default it uses the UNIX domain socket located under the/tmp/.X11-unix
directory. By mounting the/tmp/.X11-unix
directory to the same directory in the Docker container, the program (i.e. Visual Studio Code) in the Docker container can talk to the X server running on the Docker host. -
-v ~/.Xauthority:/home/$(whoami)/.Xauthority
: The.Xauthority
controls the X server access. By mounting the file on the Docker host to the Docker container, the program has access to the X server running on the Docker host. -
-v ~/.config/Code:/home/$(whoami)/.config/Code
: Mount the required directory (by Visual Studio Code) into the Docker container. -
-v ~/.vscode:/home/$(whoami)/.vscode
: Mount the required directory (by Visual Studio Code) into the Docker container. -
-v ~/Projects:/home/$(whoami)/Projects
: Assume all project codes are placed under~/Projects
, Visual Studio Code running inside the Docker container will have access to the files in the directory.-
Additional directories can be mounted. For example, adding
-v ~/Downloads:/home/$(whoami)/Downloads
will let Visual Studio Code access the~/Downloads
directory.
-
Additional directories can be mounted. For example, adding
-
--cap-add=SYS_ADMIN
: Running Visual Studio Code in Docker container requires additional Linux capabilities. -
-e DISPLAY=$DISPLAY
: This sets the value of the environment variable$DISPLAY
in the Docker container. The$DISPLAY
environment variable controls how should the program connect to the X server, which display and which screen to connect to.