windows11&WSL安装ubunutu24.04&docker

安装前准备

1、启用虚拟化与WSL功能

  • Win + R  输入 optionalfeatures.exe ,勾选 适用于 Linux 的 Windows 子系统  和 虚拟机平台 ,重启系统。
  • 验证虚拟化状态:任务管理器 → 性能 → 右下角“虚拟化”显示“已启用”。

2、设置 WSL 默认版本

  • 以管理员身份运行 PowerShell,执行:wsl --set-default-version 2 # 强制使用WSL2兼容模式

自定义位置安装Ubuntu

1、手动下载与解压

  • 访问 Microsoft WSL 分发页面,下载 .AppxBundle 安装包(如 Ubuntu 24.04)。
  • 将文件后缀改为 .zip → 解压 → 找到含 x64.appx 文件 → 再次改后缀为 .zip → 解压至目标目录(如 D:\Ubuntu)

2、初始化安装

  • 双击目录内的 ubuntu.exe ,按提示完成初始账户设置(建议使用普通用户,非 root)。

3、检查Ubuntu 安装位置:输出位置为设定盘即可

# 列出所有 WSL 发行版及其状态(用户手动执行的命令)
wsl -l -v

# 核心脚本逻辑开始
Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" | ForEach-Object {
    # 从注册表项中获取属性值
    $value = (Get-ItemProperty $_.PSPath)
    
    # 判断发行版名称是否包含 "Ubuntu"
    if ($value.DistributionName -match "Ubuntu") {
        # 输出匹配项的安装路径
        $value.BasePath
    }
}
D:\WSL\Ubuntu2204-221101\Ubuntu_2204.1.7.0_x64

系统配置与优化

更换国内镜像源

  • WSL Ubuntu 22.04(Jammy) 直接修改原始镜像源的完整步骤,支持阿里云、清华、中科大等主流镜像源
  • sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak  # 备份原文件
  • sudo vim /etc/apt/sources.list
    • 按 Esc → 输入 :wq → Enter
    • # 阿里云镜像源
      deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
    • # 清华大学镜像源
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
    • # 中科大镜像源
      deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
  • 更新软件列表
    • sudo apt update && sudo apt upgrade -y  # 强制刷新并升级系统

docker安装

卸载旧版本(若有)

sudo apt remove docker docker-engine docker.io containerd runc

安装依赖工具

sudo apt update
sudo apt install -y ca-certificates curl gnupg

添加 Docker GPG 密钥

如果遇到 NO_PUBKEY 错误:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

添加 Docker 软件源

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  noble stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装 Docker 引擎

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

查看 Docker 版本

docker --version
docker compose version

配置镜像源

vim /etc/docker/daemon.json

{
    "registry-mirrors": [
    "https://2a6bf1988cb6428c877f723ec7530dbc.mirror.swr.myhuaweicloud.com",
    "https://docker.m.daocloud.io",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com",
    "https://your_preferred_mirror",
    "https://dockerhub.icu",
    "https://docker.registry.cyou",
    "https://docker-cf.registry.cyou",
    "https://dockercf.jsdelivr.fyi",
    "https://docker.jsdelivr.fyi",
    "https://dockertest.jsdelivr.fyi",
    "https://mirror.aliyuncs.com",
    "https://dockerproxy.com",
    "https://mirror.baidubce.com",
    "https://docker.m.daocloud.io",
    "https://docker.nju.edu.cn",
    "https://docker.mirrors.sjtug.sjtu.edu.cn",
    "https://docker.mirrors.ustc.edu.cn",
    "https://mirror.iscas.ac.cn",
    "https://docker.rainbond.cc"
    ]
}
 

重新加载配置

systemctl daemon-reload

重启docekr

systemctl restart docker

允许普通用户使用 Docker

sudo usermod -aG docker $USER
newgrp docker  # 使当前会话立即生效

运行测试容器

sudo docker run hello-world

成功时会显示 Hello from Docker! 信息。

root@LENOVO:/mnt/d/data/ubuntu# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:c41088499908a59aae84b0a49c70e86f4731e588a737f1637e73c8c09d995654
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

设置开机启动

sudo systemctl enable --now docker
THE END
分享
二维码
打赏
海报
windows11&WSL安装ubunutu24.04&docker
安装前准备 1、启用虚拟化与WSL功能 按Win + R  输入 optionalfeatures.exe ,勾选 适用于 Linux 的 Windows 子系统  和 虚拟机平台 ,重启系统。 验证虚拟化……
<<上一篇
下一篇>>
文章目录
关闭
目 录