docker starts the GUI program

Posted by ram4nd on Tue, 08 Mar 2022 10:32:18 +0100

Docker's working mode is command line mode, because the main use scenario may be to do more back-end work on the server. However, sometimes we need to run some graphical interface software in the Docker container and output images. At this time, we need to solve the problem of Docker visualization.

Install docker and set up the image accelerator

curl -sSL | sh
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
  "registry-mirrors": [""]
sudo systemctl daemon-reload
sudo systemctl restart docker

GUI preparation

  1. Download x11 software in the host computer and open the permission

    sudo apt-get install x11-xserver-utils
    xhost +  
    #It is open permission and allows all users, including docker, to access the display interface of X11. Normally, it returns access control disabled and clients can connect from any host

Xhost is used to control the access rights of X server. Usually, when we log in to docker from the host and run the application on docker, as an application, the host is a client, but as a graph, it is to be displayed on the host. We need to use the Xserver of the host, so the host is a server. Therefore, before logging in to docker, you need to run xhost + on the host to enable other users to access the Xserver of the host. Xhost + is the Xserver that enables all users to access the host Xhost + ip enables users on the ip to access the Xserver of the host.

Sometimes the following errors will be reported when executing xhost

xhost:  unable to open display "XXXX"

Solution: set the DISPLAY variable, which is used to set where the graphics are displayed. The format of the DISPLAY environment variable is as follows: hostname: displaynumber Screennumber. On some machines, multiple DISPLAY devices may share the same set of input devices. For example, two CRT monitors are connected to a PC, but they share only one keyboard and one mouse. This group of DISPLAY devices has a common DISPLAY number, and each individual device in this group of DISPLAY devices has its own screen number. Both displaynumber and screennumber are numbers starting from zero. In this way, for our ordinary users, both displaynumber and screennumber are 0. Hostname refers to the hostname or ip address of the host where Xserver is located. The graph will be displayed on this machine.

export DISPLAY=:0.0
xhost +    
  1. Create image image from dockerfile

    docker build -f dockerfile -t Image name  .  
  2. Create container

    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    docker   run  -dit  -P  -e DISPLAY=$DISPLAY --privileged --network=host -v /tmp/.X11-unix:/tmp/.X11-unix:rw -v /dev/bus/usb:/dev/bus/usb   -v /home/lfxs/StudioData:/StudioData     --name gui01   mydb:0.1  /bin/bash
    #-d: Run the container in the background and return the container ID
    #-i: Run the container in interactive mode, usually at the same time as - t;
    #-P: Random port mapping: the internal port of the container is randomly mapped to the port of the host
    #-p: Specify the port mapping in the format: host port: container port
    #-e: Setting environment variables
    #--Does privileged allow the container running Docker to have root permission
    #--Network specifies the network connection type of the container, and supports bridge/host/none/container: four types; Common bridges and hosts
    #-v: Bind a volume
    #--Name: container name

Connect docker through ssh and run GUI program


ssh -Y user name@Container name -p port 

Note: specify the corresponding relationship between IP and container in / etc/hosts and add gui01. Since the network is set to hosts mode, the container and the host share one network namespace and use the IP and port of the host.

Then use the command / etc / init D / ssh status check whether ssh is enabled in the host and container. If not, use the command

systemctl restart sshd.service

Open / etc/ssh/sshd_config, add port number


Restart ssh service

/etc/init.d/ssh restart

There are also several ways to log in to docker

ssh -Y -l user name ip -p port
ssh -Y user name@ip -p port

The GUI in the test docker is displayed on the host computer, and xarclock is downloaded in the docker

 sudo apt-get install xarclock

Problems encountered

  1. When creating a container, if the network is set to host mode, specify to execute / bin/bash in the container. If not specified, the container may fail to start.
  2. Modify lightmd Conf caused the graphical interface to crash.

Topics: Docker