Install VNC in Docker container

In a headless server environment, Docker is installed and the container uses the host network mode, for example --network=host.

If you want to use a desktop environment inside your container, you can install XFCE and TigerVNC. If you want to connect to the desktop from your browser rather than through a VNC client, you also need to install noVNC.

The XFCE desktop is like this:

And the noVNC is like this:

In this example:

  • The VNC port is 5966.
  • The noVNC port is 6080.

Configuration

1. Install Packages

Install XFCE, TigerVNC, noVNC, and the required supporting packages:

bash 复制代码
sudo apt update && sudo apt install -y \
  xfce4 \
  dbus-x11 \
  tigervnc-standalone-server \
  tigervnc-common \
  novnc \
  websockify

2. Set Up the VNC Password

Set the VNC password. In this example, the password is vnc123456.

bash 复制代码
mkdir -p ~/.vnc
printf "vnc123456\nvnc123456\nn\n" | vncpasswd

3. Write the Startup File

Create the VNC startup file so that XFCE starts when you connect to the VNC session.

bash 复制代码
cat > ~/.vnc/xstartup <<'EOF'
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
export XDG_SESSION_TYPE=x11
exec startxfce4
EOF

chmod +x ~/.vnc/xstartup

Start and Connect

1. Start the VNC Server

Start the VNC server and set the port to 5966.

bash 复制代码
vncserver :66

The display number :66 maps to port 5966. After the VNC server starts, you can connect to the desktop with a VNC client, for example by creating a VNC session in MobaXterm.

Notice: After running vncserver :66 to start the VNC service, the current terminal may become very slow or less responsive. If you need to run other commands, open a new terminal and execute them there.

For example, when I use a container, I usually first connect to the host machine through SSH with MobaXterm. Then, on the host machine, I create a new tmux session. Inside that tmux session, I use one window to SSH into the container and start the VNC service. After the VNC service is started, I switch to other tmux windows to run the other commands I need. The tmux window that was used to start the VNC service is left idle. Do not close this window, because closing it will also stop the VNC service, even if you do not explicitly run the command to kill the VNC server.

2. Stop the VNC Server

If you want to stop the VNC server on port 5966:

bash 复制代码
vncserver -kill :66

3. Start noVNC for Browser Access

After starting the VNC server, start noVNC to allow browser-based access to the desktop:

bash 复制代码
websockify --web=/usr/share/novnc/ 0.0.0.0:6080 localhost:5966

Then use the following URL to connect:

text 复制代码
http://IP:6080/vnc.html

Replace IP with your actual IP address, for example:

text 复制代码
http://192.168.1.2:6080/vnc.html

4. Show Running VNC Services

To check the running VNC services, you can use the following command:

bash 复制代码
vncserver -list
相关推荐
木雷坞1 小时前
Docker 镜像源列表(11月30日更新-长期)
运维·docker·容器
迪康软件zz3 小时前
单向阀的代码:把"高级别解低级别"写成可测的判定
运维·安全·自动化运维
SeanQhl3 小时前
容器隔离与资源限制实操
容器·cgroup·openeuler·namespace·资源隔离
Diligently_3 小时前
docker 中的常见的使用命令(三)
运维·docker·容器
我不会起名字3224 小时前
SLO 与错误预算:为什么 99.99% 往往不是好目标?
运维·架构设计·告警·可观测性·slo
易知微EasyV数据可视化5 小时前
多路监控视频与三维场景融合展示
运维·视频孪生·三维场景·安全健康
Decay__5 小时前
PostgreSQL 优化案例集 —— 慢 SQL 优化、索引重构与参数调整收益对比
运维·数据库·sql·postgresql
7177776 小时前
信创 DevOps 平台选哪家:Gitee 与主流方案的适配维度对比
运维·gitee·devops
-梅6 小时前
linux网络(1)网络聊天室与网络计算器
linux·运维·服务器
wno70415 小时前
Nginx禁止蜘蛛程序访问
运维·nginx