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
相关推荐
RisunJan8 小时前
Linux命令-rlogin(远程登录)
linux·运维
深圳恒讯8 小时前
菲律宾云服务器与传统VPS的架构差异
运维·服务器·架构
蓝天下的守望者8 小时前
svt_apb_if里的宏定义问题
运维·服务器·数据库
小林ixn8 小时前
从“玩具工具”到“跨语言利器”:MCP 协议实战解析
运维·服务器·网络
小顿的企业观察9 小时前
中企出海战略规划,正在从“走出去”转向“走进去”
大数据·运维·人工智能·产品运营·制造
数智化转型推荐官10 小时前
2026统一身份管理系统五大发展趋势解读
java·运维·微服务
看菜鸡互10 小时前
探索用 SlideML 让大模型生成 PPT 的实验方法
java·运维
liguojun202512 小时前
篮球馆自动计时收费系统:从规则配置到自动结算的全流程拆解
java·大数据·运维·人工智能·物联网·1024程序员节
Promise微笑12 小时前
红外分辨率 160×120、320×240、384×288 与 640×480实战选型指南
大数据·运维·人工智能·物联网
云飞云共享云桌面13 小时前
单机建模卡顿运维繁琐!中小型机械制造厂云飞云 3D 云桌面落地
运维·服务器·网络·3d·自动化·电脑·负载均衡