WSL调用docker

WSL(windows subsystem linux)是window系统的原生linux子系统,用于代码开发很方便。

希望在wsl里面运行docker,首先要安装docker在WSL中使用,大部分人的第一想法肯定是用以下命令行安装(个人不推荐)

 # 更新软件包列表
sudo apt-get update
 
# 安装Docker
sudo apt-get install docker
 
# 确认Docker安装成功并运行
sudo systemctl status docker

我不知道上面的命令是否真的能安装,但反正我折腾了很长时间,最后没搞定wsl里的docker,所以我给出另一种方案。

首先你需要安装docker desktop这个免费软件,从下面链接下载:

Docker Desktop: The #1 Containerization Tool for Developers | DockerDocker Desktop is collaborative containerization software for developers. Get started and download Docker Desktop today on Mac, Windows, or Linux.icon-default.png?t=N7T8https://www.docker.com/products/docker-desktop/安装时候记得勾选WSL选项,大概如下:

图形界面安装很简单,安装完成之后分别在powershell和wsl中执行以下命令得到如下回复即安装成功。(需要注意的是在wsl中使用root用户执行,或者加sudo前缀。)

$docker run hello-world

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

powershell 是windows的命令行一般没什么问题,wsl如果无法执行可以设置成WSL2,WSL1似乎不支持docker desktop

wsl --set-default-version 2

我的windows电脑安装了两种WSL,一种是ubuntu(通过cmd的wsl install命令安装),一种是ubuntu24(通过应用商店)但无论我如何折腾,我都没办法直接通过点击红色的ubuntu24图标进入的wsl 命令行运行docker,结果总是

¥docker ps

The command 'docker' could not be found in this WSL 2 distro.
We recommend to activate the WSL integration in Docker Desktop settings.

For details about using Docker Desktop with WSL 2, visit:

https://docs.docker.com/go/wsl2/

但在我尝试如下命令将Ubuntu-24.04设为默认wsl的之后,Ubuntu-24.04中的docker也可用了,虽然不知道发生了什么。

wsl --set-default Ubuntu-24.04

如果你遇到了WSL中使用docker的问题,多尝试设置,多重启,最后可以参考以下网页

Docker Desktop WSL 2 backend on Windows | Docker DocsTurn on the Docker WSL 2 backend and get to work using best practices, GPU support, and more in this thorough guide.icon-default.png?t=N7T8https://docs.docker.com/desktop/wsl/ 

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值