YunchaoYang / Blogs

blogs and notes, https://yunchaoyang.github.io/blogs/
0 stars 0 forks source link

How to use Container for GUI Applications #45

Open YunchaoYang opened 1 year ago

YunchaoYang commented 1 year ago

Intro

Running GUI applications in a Docker or Singularity container can be achieved by following a few key steps. The mechanism involves configuring the container to share the host system's display and setting up necessary environment variables to ensure the GUI application interacts with the host's X Window System (X11). Here's a general guide on how to do this:

1. Enable X11 Forwarding

For Docker Containers:

To run a docker containerized GUI application, you need to enable X11 forwarding to allow the container to diapla GUI on your host system. In Docker, use the -e flag to set the argument variables: -e DISPLAY=$DISPLAY and -v /tmp/.X11-unix:/tmp/.X11-unix when starting the docker container. Make sure that X client () can be connected from any host ? xhost + Example from Lei's blog:

1. build a firefox docker image

2. start Firefox from the Docker container

$ xhost +
$ docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox:0.0.1
$ xhost -

3. Display Size

We could set the display size using environment variables DISPLAY_WIDTH and DISPLAY_HEIGHT.

 docker run -it --rm -e DISPLAY=$DISPLAY -e DISPLAY_WIDTH=3840 -e DISPLAY_HEIGHT=2160 -v /tmp/.X11-unix:/tmp/.X11-unix firefox:0.0.1

For Singularity/AppTainers:

In Singularity, you can use the --nv or --x11 flags to enable X11 support.

singularity run --x11 <your-image>

2 Configure X Authorization

Depending on your system's security settings, you may need to configure X authorization to allow the container to access the X server. This can involve using xhost to add permissions for the container's user to access the X server. However, be cautious with this approach, as it may have security implications. For example, you can allow any host to connect to the X server (not recommended for production systems):

xhost +

3 Run the GUI Application:

Inside the running container, you can start your GUI application as you would on the host system. The application should now display its graphical interface on the host's X server.

Clean Up (Optional):

After running the GUI application in the container, you can revoke X authorization or stop the X server, depending on your security requirements. To revoke X authorization (if you used xhost + earlier):

xhost -

references

  1. Lei Mao's blog https://leimao.github.io/blog/Docker-Container-GUI-Display/