在Windows系统下,配置WSL2与安装Windows Docker
附个人博客本页地址:
https://zhwuyuhehe.github.io/2023/08/17/WSL与Docker/
配置Windows功能
打开Windows功能面板
勾选适用于Linux的Windows子系统
与虚拟机平台
这两个功能
勾选完成后,点击确定。有可能需要重启,重启即可。
配置WSL2,安装Ubuntu
安装与初始化
按下Windows+X,选择Windows PowerShell(管理员)。
此后的所有CMD指令都需要管理员权限,每次都需要选择管理员模式打开。
在powershell中输入wsl --set-default-version 2
,设置WSL版本为2。
在微软商店中搜索Ubuntu
,选择Ubuntu 22.04.2 LTS
,点击获取。
完成安装后打开,进行初始化配置,输入你的用户名和密码,此密码同时作为管理员密码。
输入指令sudo apt update
,更新apt源。随后输入sudo apt upgrade
,升级apt源。
输入apt install language-pack-zh-hans*
,安装中文语言包。
输入cd /etc/default
,进入语言设置文件目录。
输入sudo vim locale
,编辑语言设置文件。
在文件中找到LANG
,将其值修改为LANG="zh_CN.utf8"
,保存并退出。
关闭控制台窗口,重新打开一个power shell窗口,输入wsl --shutdown
,关闭WSL。
再次输入wsl,打开WSL,输入locale
,查看语言设置是否成功。
如果你习惯或需要systemd来管理你的WSL
在Ubuntu中输入echo -e "[boot]\nsystemd = true" > /etc/wsl.conf
,创建WSL配置文件。
在powershell中输入wsl --shutdown
,关闭WSL。
输入wsl --update
,更新WSL。此时会安装Windows Subsystem for Linux。
安装完成后,输入wsl
,打开WSL,可以使用命令进行确认:systemctl list-unit-files --type=service
,该命令将显示服务的状态。
下载与安装Docker
在官网上下载Docker Desktop for Windows,安装完成后,打开Docker Desktop。
在上方菜单栏中选择齿轮按钮,打开设置。在左侧菜单栏中点击Resources选择WSL Integration。
在右侧的WSL Integration中,勾选Ubuntu-22.04,点击Apply & Restart。
如果你的C盘容量告急,在Resources中找到Advanced,点击进入。
把Disk image location
修改为你想要的位置,点击Apply & Restart。
完成与测试
在powershell中输入wsl
,打开WSL。
查看Docker版本
输入docker version
,查看Docker版本。
$ docker version
Client: Docker Engine - Community
Cloud integration: v1.0.35-desktop+001
Version: 24.0.5
API version: 1.43
Go version: go1.20.6
Git commit: ced0996
Built: Fri Jul 21 20:35:45 2023
OS/Arch: linux/amd64
Context: default
Server: Docker Desktop
Engine:
Version: 24.0.5
API version: 1.43 (minimum version 1.12)
Go version: go1.20.6
Git commit: a61e2b4
Built: Fri Jul 21 20:35:45 2023
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit: v1.1.7-0-g860f061
docker-init:
Version: 0.19.0
GitCommit: de40ad0
测试Docker运行Hello World
输入docker run hello-world
,测试Docker是否正常运行。
$ docker run hello-world
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/
恭喜你,你已经成功配置了WSL2与Docker。🎉🎉🎉