欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Windows10在wsl2上面安裝docker全攻略

 更新時(shí)間:2025年11月21日 09:36:48   作者:楽焫旒璡  
作為開發(fā)者,你可能會遇到不同操作系統(tǒng)和環(huán)境帶來的挑戰(zhàn),Docker 可以幫助你創(chuàng)建隔離和可移植的容器,使應(yīng)用程序運(yùn)行得更加高效,這篇文章主要介紹了Windows10在wsl2上面安裝docker的相關(guān)資料,需要的朋友可以參考下

1.安裝wsl2

WSL2 是 “運(yùn)行環(huán)境”:全稱 Windows Subsystem for Linux 2,是 Windows 系統(tǒng)自帶的功能。它通過虛擬化技術(shù),讓 Windows 能原生運(yùn)行 Linux 內(nèi)核,無需單獨(dú)裝虛擬機(jī)(如 VMware),啟動快、資源占用低。

查看是否安裝 wsl -v

PS C:\Users\guyin> wsl -v
WSL 版本: 2.6.1.0
內(nèi)核版本: 6.6.87.2-1
WSLg 版本: 1.0.66
MSRDC 版本: 1.2.6353
Direct3D 版本: 1.611.1-81528511
DXCore 版本: 10.0.26100.1-240331-1435.ge-release
Windows: 10.0.26100.6899

若未安裝, 右鍵開始-搜索-wsl, 若未安裝wsl按下任意鍵自動下載安裝, 或者powershell執(zhí)行"wsl --install"自動下載安裝并啟用wsl2

2.升級Windows系統(tǒng)

我的系統(tǒng)是win11, 規(guī)格如下:

版本    Windows 11 家庭中文版
版本號    24H2
安裝日期    ‎2025/‎8/‎11
操作系統(tǒng)版本    26100.6899

如果操作系統(tǒng)版本為"10.0.26100.6899", 需要升級系統(tǒng), 這個版本的系統(tǒng)無法安裝Ubuntu, 升級后最后. "10.0.26100.7171"是可以安裝ubuntu的(已驗(yàn)證).

3.安裝ubuntu

Ubuntu 是 “適配的系統(tǒng)”:Ubuntu 是主流 Linux 發(fā)行版之一,微軟已將其打包成應(yīng)用,可在微軟商店下載。安裝后,它會作為 WSL2 的 “客戶端”,借助 WSL2 的內(nèi)核環(huán)境,實(shí)現(xiàn)和物理機(jī) Ubuntu 幾乎一致的命令行操作、軟件安裝(如 Docker、Python)。

3.1.確保BIOS/UEFI 中啟用了虛擬化(VT-x / AMD-V)

如何檢查?

  1. 按 Ctrl + Shift + Esc 打開任務(wù)管理器
  2. 切到 “性能” → “CPU”
  3. 查看右下角:“虛擬化: 已啟用”

如果顯示 “已啟用” → 跳到第二步
如果顯示 “已禁用” → 需要進(jìn) BIOS 開啟

如何開啟虛擬化?

  • 重啟電腦,按 F2 / F10 / Del / Esc(不同品牌鍵不同)進(jìn)入 BIOS
  • 找到類似選項(xiàng):
    • Intel CPU: Intel Virtualization Technology (VT-x) → Enabled
    • AMD CPU: SVM Mode 或 AMD-V → Enabled
  • 保存并退出(通常按 F10)

?? 筆記本常見位置:

  • Dell: Processor Settings → Virtualization
  • Lenovo: Security → Virtualization
  • HP: System Configuration → Virtualization Technology

3.2.確保 Windows 功能已啟用

管理員身份 打開 PowerShell 或 CMD,運(yùn)行:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

然后重啟電腦。

?? 注意:必須啟用 VirtualMachinePlatform,僅啟用 WSL 是不夠的!

你也可以通過圖形界面檢查:

  1. 按 Win + R → 輸入 optionalfeatures
  2. 確保勾選:
    • ?? 適用于 Linux 的 Windows 子系統(tǒng)
    • ?? 虛擬機(jī)平臺
  3. 點(diǎn)確定并重啟

3.3.設(shè)置 WSL2 為默認(rèn)版本

重啟后,在 PowerShell 中運(yùn)行:

wsl --set-default-version 2

3.4.安裝 Ubuntu

現(xiàn)在通過 Microsoft Store 安裝 Ubuntu):

  1. 打開 Microsoft Store
  2. 搜索 “Ubuntu”
  3. 安裝 Ubuntu(由 Canonical 提供)
  4. 安裝完成后,從開始菜單啟動它

打開之后要求創(chuàng)建普通用戶和設(shè)置密碼, root操作使用sudo執(zhí)行

3.5.驗(yàn)證是否成功

安裝成功后,運(yùn)行:

wsl -l -v

輸出應(yīng)類似:

  NAME      STATE      VERSION
* Ubuntu    Running    2

進(jìn)入 WSL(默認(rèn)進(jìn)入powershell所在目錄掛載的位置):

wsl

并測試:

uname -a  # 應(yīng)包含 "microsoft" 和 "WSL2"

3.6.補(bǔ)充說明

  • 即使你在 Windows 11 Insider(Build 26100),也必須開啟虛擬化,否則 WSL2 無法工作。
  • 某些殺毒軟件(如 360、McAfee)會阻止虛擬化,可臨時(shí)關(guān)閉測試。
  • Hyper-V 不是必須的,但 VirtualMachinePlatform 是 WSL2 的核心依賴。

4.遷移ubuntu工作目錄

4.1.目錄掛載說明

ubuntu系統(tǒng)文件目錄如/home, /usr 是在C盤

  • Windows 的各個盤符自動掛載在 WSL2 的 /mnt/ 目錄下:
/mnt/c/   → 對應(yīng) Windows 的 C:\  
/mnt/d/   → 對應(yīng) D:\  
...
  • 例如:Windows 上的 C:\Users\guyin\Documents
    在 Ubuntu 中路徑是:
/mnt/c/Users/guyin/Documents

?? 注意:不要在 /mnt/c/... 下運(yùn)行 Python 項(xiàng)目或容器!
因?yàn)?NTFS ↔ Linux 文件系統(tǒng)跨層訪問會導(dǎo)致:

  • 文件監(jiān)聽失效(如 Flask 自動重載不工作)
  • 權(quán)限問題
  • I/O 性能大幅下降

最佳實(shí)踐:把代碼放在 Ubuntu 自己的文件系統(tǒng)里(如 /home/guyin/myproject

4.2.遷移目錄

打開powershell, 創(chuàng)建目錄并進(jìn)入

mkdir E:\ws2_space
cd E:\ws2_space

導(dǎo)出ubuntu

wsl --export Ubuntu ubuntu.tar

注銷(刪除)當(dāng)前發(fā)行版

wsl --unregister Ubuntu

從 tar 重新導(dǎo)入到 E 盤(注意路徑)

wsl --import Ubuntu E:\ws2_space\Ubuntu ubuntu.tar --version 2

設(shè)置默認(rèn)用戶(否則會以 root 登錄)

# 創(chuàng)建 /etc/wsl.conf 或運(yùn)行:
ubuntu config --default-user guyin   # 如果是 Ubuntu 官方包

4.3.Windows文件資源管理器打開ubuntu系統(tǒng)目錄

地址欄輸入以下文本回車即可

\\wsl$\Ubuntu

5.安裝docker

需進(jìn)入wsl, powershell執(zhí)行"wsl"

"cat /proc/1/comm" 命令輸出了"systemd"說明systemd已啟用

5.1.啟動systemd(需要確認(rèn)版本是否需要手動配置)

Windows 11 22H2 + WSL 內(nèi)核 ≥ 5.10.67+ 開始,微軟在部分版本中默認(rèn)啟用了 systemd 支持,無需手動配置 [wsl2]\nsystemd=true

若要配置可能由于編碼相關(guān)問題出錯自行搜索即可.

  1. 按 Win + R,輸入 notepad 回車
  2. 輸入兩行內(nèi)容:
[wsl2]
systemd=true
  1. 點(diǎn)擊 文件 → 另存為
  2. 文件名:$env:USERPROFILE\.wslconfig
  3. 保存類型:所有文件 (.)
  4. 編碼:選擇 “UTF-8”(注意:Windows 記事本的 “UTF-8” 實(shí)際是 UTF-8 無 BOM,盡管名字沒寫清楚)
  5. 點(diǎn)“保存”

5.2.安裝docker

安裝依賴

# 安裝依賴
sudo apt update
sudo apt install -y ca-certificates curl gnupg lsb-release

下載GPG密鑰(下載報(bào)錯就多試幾次)

# 1. 下載 GPG 密鑰到臨時(shí)文件
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o docker.gpg

# 2. 檢查是否下載成功(應(yīng)顯示 "Public Key")
file docker.gpg

# 3. 轉(zhuǎn)換為 gpg 格式并安裝
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg docker.gpg

# 4. 刪除臨時(shí)文件
rm docker.gpg

添加倉庫 & 安裝 Docker

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

# 更新包列表
sudo apt update

# 安裝 Docker Engine + Compose plugin
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

安裝docker

# 安裝 Docker Engine
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

# 將當(dāng)前用戶加入 docker 組(避免每次 sudo)(需要重啟wsl2)
sudo usermod -aG docker $USER

設(shè)置開機(jī)自啟并設(shè)置鏡像倉庫地址

# 切換root
sudo su -

# 設(shè)置開機(jī)自啟
systemctl enable docker.service
# 查看Docker狀態(tài)
systemctl status docker

# 設(shè)置docekr鏡像
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://docker-0.unsee.tech",
    "https://docker.m.daocloud.io"
  ],
  "live-restore": true,
  "features": { "buildkit": true }
}
EOF

# 重啟docker
systemctl restart docker
 
# 查看docker狀態(tài)
systemctl status docker

# 退出root
exit

退出WSL2

exit

重啟 WSL2

wsl --shutdown

然后重新打開 Ubuntu 終端。

wsl

驗(yàn)證

docker run --rm hello-world
docker-compose --version

如下說明安裝成功

ly@localhost:/mnt/e/ws2_space$ docker run --rm hello-world
docker compose version
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete
Digest: sha256:f7931603f70e13dbd844253370742c4fc4202d290c80442b2e68706d8f33ce26
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/

Docker Compose version v2.40.3

總結(jié) 

到此這篇關(guān)于Windows10在wsl2上面安裝docker的文章就介紹到這了,更多相關(guān)Windows10在wsl2安裝docker內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論