Running GUI App -VSCode in a Docker Container
Why? Because why not! I am a big fan of docker and find it really useful not just to launch containers for production, but also to launch applications that I don’t want to install. So let’s go!
Docker has been a really popular tool since its launch in the market and has moved the concept of Virtualisation to a different era. Containerisation has increased the pace of deployment and reduced the time to market for most companies in the technological world.
But according to me, “Docker” has limited the thinking of people to launch containers for production and not do something far more interesting it is capable of!
Theoretically, you can easily launch services in a docker container in 1 second.
But that will be CLI-based.
💡 Now, Think from a different perspective.
This is not a use-case of docker, I know, but think…
What if you want to launch a desktop application, use it for a while, test it, play around with the software, you don’t want to take the pain of downloading & installing it on your Bare Metal.
You can use Docker here!!
It will let you launch an application/service in 1 second without downloading.
The only thing to THINK about is, how to bring the GUI.
Let's get started!
Pre-requisites:
- Host OS should be GUI
- Docker installed in your OS
Host OS should be GUI as Host OS will share the DISPLAY with the Docker container, and the container will assume it has a display.
I have used the concept of X11 forwarding so that I can share the display between the Host & the Container.
Dockerfile
FROM debian:bullseye-slimRUN apt-get update && apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
--no-install-recommendsRUN curl -sSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | apt-key add - RUN echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable ma in" > /etc/apt/sources.list.d/vscode.listRUN apt-get update && apt-get -y install \
code git \
libasound2 \
libatk1.0-0 \
libcairo2 \
libcups2 \
libexpat1 \
libfontconfig1 \
libfreetype6 \
libgtk2.0-0 \
libpango-1.0-0 \
libx11-xcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
libxshmfence-dev\
openssh-client \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
RUN mkdir /root/codedata
RUN mkdir /root/mydata
CMD [ "/usr/bin/code","--user-data-dir=/root/codedata","--verbose" ]
- The base image is Debian-based.
- To launch VSCode inside container & GUI on a shared host, multiple dependencies are installed.
- Based on the research I did, many lib* packages were needed for GUI on the container, libxshmfence-dev was the final package that debugged the error.
- — no-install-recommends
is used in apt-get to avoid installing recommended packages as a dependency.- 2 folders are created: 1 for data of VSCode and the other for the code that will be put by the user to be used in the VSCode.
➡ While launching the container user will mount his code files in the /root/mydata
folder. So when VSCode starts he can use his files and save the output in the same folder.
# docker build -t dakshjain09/docker-gui-vscode:v1 .
The image is in my DockerHub account — dakshjain09/docker-gui-vscode:v1
To use the image to launch a container follow these steps:
- Use a Linux-hosted VM. [I have not yet tested it on Windows/MacOS]
- Put your code files in the host machine (Centos7) directory.
For this article, I am putting a test Dataset and a Machine Learning Python code to do processing on the container on VSCode.
# mkdir mypy
In this folder, I have put the dataset & the python ML code.
Now, my image has VSCode in it.
Based on the user requirement they can use my image for their own purpose. For my example, I need Python installed inside the container & a few libraries.
So I have created another custom Dockerfile.
# docker build vscode-python:v1 .
This new image build in my localhost.
- Run this command:
# docker run -it --net=host --env DISPLAY=':0' -v /root/GUI/mypy:/root/mydata vscode-python:v1
In this article we learned:
- Think Outside the Box 📦
- Think creative 💫
- Docker containers can be used to launch GUI Applications 💻
- Use my VSCode image for your use case by building a custom Dockerfile.