发布于 2026-01-06 3 阅读
0

在适用于 Linux 的 Windows 子系统 v2 (Ubuntu) 上安装 Docker

在适用于 Linux 的 Windows 子系统 v2 (Ubuntu) 上安装 Docker

在适用于 Linux 的 Windows 子系统 v2 (Ubuntu) 上安装 Docker

在适用于 Linux 的 Windows 子系统 v2 (Ubuntu) 上安装 Docker

Windows 10 用户现在可以预览适用于 Linux 的 Windows 子系统 2 (WSL2)。WSL2 相较于 WSL 有了显著改进,文件系统性能大幅提升,并具备完整的系统调用功能。这意味着我们终于可以在 WSL 中运行 dockerd 了!

WSL2 目前仅作为预览功能通过Windows Insider 计划提供。WSL2 需要 Windows 10 版本 18917 或更高版本,而该版本目前需要 Windows Insider 的“快速通道”。

如果您尚未准备好在开发机上运行快速预览版,您可以下载 ISO 镜像并在虚拟机中体验 WSL2。Windows Insider ISO 镜像可在此处获取:

设置 WSL2

WSL2 的安装说明请参见此处:请确保从Microsoft Store安装 Ubuntu 发行版。

设置完成后,启动命令提示符并运行以下命令以验证 Ubuntu 是否设置为版本 2。


# Set WSL to default to v2
wsl --set-default-version 2

# check the version
wsl -l -v

# Output should show Ubuntu and version 2
# if not, you can upgrade the distro
# this usually takes 5-10 minutes
wsl --set-version Ubuntu 2

安装 Docker

在 WSL bash 中运行以下命令来设置 Docker。这些命令与在 Ubuntu 虚拟机上设置 Docker 的步骤几乎完全相同。


# update the package manager and install some prerequisites (all of these aren't technically required)
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common libssl-dev libffi-dev git wget nano

# create a group named docker and add yourself to it
#   so that we don't have to type sudo docker every time
#   note you will need to logout and login before this takes affect (which we do later)
sudo groupadd docker
sudo usermod -aG docker ${USER}

# add Docker key and repo
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# (optional) add kubectl key and repo
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

# update the package manager with the new repos
sudo apt-get update

# upgrade the distro
sudo apt-get upgrade -y
sudo apt-get autoremove -y

# install docker
sudo apt-get install -y docker-ce containerd.io

# (optional) install kubectl
sudo apt-get install -y kubectl

# (optional) install latest version of docker compose
sudo curl -sSL https://github.com/docker/compose/releases/download/`curl -s https://github.com/docker/compose/tags | \
grep "compose/releases/tag" | sed -r 's|.*([0-9]+\.[0-9]+\.[0-9]+).*|\1|p' | head -n 1`/docker-compose-`uname -s`-`uname -m` \
-o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose

完成设置

目前 Docker 不会自动启动,因此每次都需要手动启动该服务。以下命令会修改您的 .profile 文件,以便每次登录时 Docker 都能自动启动。


echo "sudo service docker start" >> ~/.profile

# exit and then restart WSL
exit

退出并重启 WSL(只需在命令提示符或 Windows 运行命令中运行“wsl”)后,Docker 应该就能正常工作了。要进行测试,请在 WSL bash 中运行以下命令。


docker run -it hello-world

额外赠品

我希望 WSL 始终从我的主目录 (~) 启动,所以我使用以下命令将其添加到我的 .profile 文件中。


echo "cd ~" >> ~/.profile

安装 Azure CLI


# add Azure CLI key and repo
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg
CLI_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ ${CLI_REPO} main" | sudo tee /etc/apt/sources.list.d/azure-cli.list

# update the package manager
sudo apt-get update

# install Azure CLI
sudo apt-get install -y azure-cli

安装 .NET Core


# add the dotnet core repo
echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-bionic-prod bionic main" | sudo tee /etc/apt/sources.list.d/dotnetdev.list

# update the package manager
sudo apt-get update

# install dotnet core
sudo apt-get install -y dotnet-sdk-2.2

文章来源:https://dev.to/bartr/install-docker-on-windows-subsystem-for-linux-v2-ubuntu-5dl7