win11的docker部署Ubuntu26

1、Dockerfile配置

FROM ubuntu:26.04

ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai

# 1. 换源 + 安装基础包(sudo、tzdata、tigervnc-tools)
RUN apt-get update -o Acquire::AllowInsecureRepositories=true || true && \
    apt-get install -y --no-install-recommends ca-certificates && \
    \
    sed -i 's|http://archive.ubuntu.com/ubuntu|http://mirrors.aliyun.com/ubuntu|g' /etc/apt/sources.list 2>/dev/null || true && \
    sed -i 's|http://security.ubuntu.com/ubuntu|http://mirrors.aliyun.com/ubuntu|g' /etc/apt/sources.list 2>/dev/null || true && \
    for f in /etc/apt/sources.list.d/*.sources; do \
        [ -f "$f" ] && sed -i 's|http://archive.ubuntu.com/ubuntu|http://mirrors.aliyun.com/ubuntu|g' "$f"; \
        [ -f "$f" ] && sed -i 's|http://security.ubuntu.com/ubuntu|http://mirrors.aliyun.com/ubuntu|g' "$f"; \
    done 2>/dev/null || true && \
    \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        sudo tzdata tigervnc-tools && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    echo $TZ > /etc/timezone && \
    dpkg-reconfigure -f noninteractive tzdata && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# 2. 配置用户 ubuntu/ubuntu
RUN echo "ubuntu:ubuntu" | chpasswd && \
    usermod -aG sudo ubuntu && \
    echo "ubuntu ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/ubuntu && \
    chmod 0440 /etc/sudoers.d/ubuntu

# 3. 安装 XFCE4 + TigerVNC 服务端 + noVNC
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    xfce4 xfce4-goodies \
    tigervnc-standalone-server \
    tigervnc-viewer \
    novnc \
    websockify \
    dbus-x11 \
    libexo-2-0 \
    xfonts-base \
    fonts-noto-cjk \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# 4. 以 ubuntu 用户配置 VNC(Ubuntu 26.04 默认密码路径 ~/.config/tigervnc/passwd)
USER ubuntu
WORKDIR /home/ubuntu

RUN mkdir -p ~/.vnc ~/.config/tigervnc && \
    echo "ubuntu" | vncpasswd -f > ~/.config/tigervnc/passwd && \
    chmod 600 ~/.config/tigervnc/passwd && \
    printf '%s\n' \
        '#!/bin/bash' \
        'unset SESSION_MANAGER' \
        'unset DBUS_SESSION_BUS_ADDRESS' \
        'exec dbus-launch --exit-with-session startxfce4' \
        > ~/.vnc/xstartup && \
    chmod +x ~/.vnc/xstartup

# 5. 入口
USER root
EXPOSE 5901 6080

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
# docker run -d -p 5901:5901 -p 6080:6080 --name ubuntu ubuntu

2、entrypoint.sh配置

#!/bin/bash
set -e

# 时区(运行时可 -e TZ=America/New_York 覆盖)
TZ=${TZ:-Asia/Shanghai}
export TZ
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
echo $TZ > /etc/timezone

# 清理旧锁
rm -f /tmp/.X1-lock /tmp/.X11-unix/X1 2>/dev/null || true
rm -f /home/ubuntu/.vnc/*.pid /home/ubuntu/.vnc/*.log 2>/dev/null || true

# 修正权限
chown -R ubuntu:ubuntu /home/ubuntu/.vnc /home/ubuntu/.config/tigervnc

# 启动 VNC(Ubuntu 26.04 必须显式 -xstartup,密码文件用 -rfbauth 指定)
su - ubuntu -c "tigervncserver :1 -geometry 1920x1080 -depth 24 -xstartup ~/.vnc/xstartup -rfbauth ~/.config/tigervnc/passwd -localhost no"

sleep 2

# 启动 noVNC
websockify -D --web=/usr/share/novnc/ 0.0.0.0:6080 localhost:5901

echo "========================================"
echo "  Ubuntu 26.04 Desktop 已启动"
echo "  VNC:     localhost:5901"
echo "  noVNC:   http://localhost:6080/vnc.html"
echo "  用户:    ubuntu"
echo "  密码:    ubuntu"
echo "  时区:    $(date)"
echo "========================================"

tail -f /dev/null

3、构建

docker build -t ubuntu .

4、启动

docker run -d -p 5901:5901 -p 6080:6080 --name ubuntu ubuntu
posted @ 2026-08-09 12:12  旧色染新烟  阅读(11)  评论(0)    收藏  举报