0%

WSL2搭建docker集成IDEA

Windows 利用 WSL2 安装

适用于 Linux 文档的 Windows 子系统

安装过程的问题解决:

我遇到的问题:

问题一:

在管理员PowerShell里运行

1
wsl --install

结果出现如下问题:

wsl –install 正在安装: 虚拟机平台 已安装 虚拟机平台。 正在安装: 适用于 Linux 的 Windows 子系统 已安装 适用于 Linux 的 Windows 子系统。 正在下载: WSL 内核 安装过程中遇到错误,但可以继续安装。组件: ‘WSL 内核’ 错误代码: 0x80072f78

解决方法:

勾选“Windows虚拟机监控程序平台”。然后卸载WSL(勾除“适用于Linux的Windows子系统”)、重启、再重装WSL(勾选“适用于Linux的Windows子系统”/输入命令wsl –install)即可。

问题二:

安装Ubantu时,出现以下错误:

1
wsl --install -d Ubuntu-20.04

正在下载: Ubuntu-20.04
安装过程中出现错误。分发名称: ‘Ubuntu-20.04’ 错误代码: 0x80072f7d

解决办法:

直接在Microso Store里安装Ubuntu

还有问题:请自行谷歌解决 Windows10 启动WSL2 并安装Ubuntu遇到的一系列问题

WSL2安装docker

修改国内镜像源

要选择版本相对应的!

参考Ubuntu 安装docker

注意:

  1. 启动命令有所不同

    1
    sudo service docker start 而不是sudo systemctl restart docker
  2. Docker 镜像加速

    阿里云镜像获取地

  3. Docker 作为非 root 用户,则应考虑使用类似以下方式将用户添加到 docker 组(建议设置)

    适用于 Linux 的安装后步骤

Docker 安装是否正常

1
2
3
4
5
6
7
8
9
10
11
## 检查dockerd进程启动
service docker status
ps aux|grep docker

## 查看 Docker 版本
docker version

## 检查拉取镜像是否正常
docker pull mysql
docker images

测试

1
sudo docker run hello-world

应当出现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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正常运行!

Docker Desktop for windows方式

这里要注意了 ,官方给出两种方式,因为我是用wsl2构建虚拟容器的,选择wsl2后端! 这种方案性能更好!

在 Windows 上安装 Docker Desktop

以交互方式🔗安装

启动 Docker 桌面

按照图像化界面的指示就阔以了,中途没有成功不用惊慌,这只是教你如何基本使用!

如何你要成功运行案例,可以需要配置 科学上网 代理工具!

以这种方式安装,docker 默认使用WSL2 的linux 容器

也就是说 构建的容器 都是在WSL2 中!

解决网络问题

这一步就是 重中之重了!后续所有 IDEA 开发 都要打通与WSL2 中的docker !!

因为每一次 启动 wsl2 ip 都在变化! 所以就有众多方案!

看看网上怎么说:

  1. ubuntu 子系统中添加一个新的网卡来给宿主机连接用 FAQ
  2. 实现WSL2固定IP效果+开机启动

说也说去,核心就是 ip 要固定,这是我们去设置的,在官方给出的方案 window10 只需要通过localhost:2375 就可以访问到wsl2 里面的docker.原因在于docker for desktop 帮助我们做了host映射 就不用关注docker 了!要实现 只需要点击一下就好:

到此就完成了网络的联通!

IDEA开发调试

这里跟 Linux ssh 连接不同 选择 tcp 套接字,当然 你如果提前知道wsl2 中 ip 并且开启 远程访问 你也是可以用ssh 连接的,其实我们部署到aliyun 服务器 就是采用这种方式!

到此 所有准备工作就完毕来!

尝试在IDEA 中拉取 image

你会发现 拉取的镜像 在左边 出现 同时 desktop 和 wls2 中的docker 都出现了!

你可以通过日益一个位置 管理!

来进行一波测试的效果吧!

Docker 实例

但是你 以为就完了吗!

在 实例化 redis 是 容器失败

docker [An attempt was made to access a socket in a way forbidden by its access permissions] 解决方案

github 解决方案

结合第三方

再次测试 成功部署!