使用WSL2+Docker在Windows 10上运行镜像及访问容器实例

1,129 阅读3分钟

为啥会有这篇文章?

想买mac -> 没钱 -> 装黑苹果 -> 发现CPU是AMD的 :(

Linux环境准备

  1. 运行 -> control -> 程序和功能 -> 启用或关闭Windows功能 -> 适用于Windows的Linux子系统
  2. 从 github 下载 Windows Terminal , 使用 powershell 安装
add-appxpackage ./Microsoft.WindowsTerminal_<版本号>.msixbundle
  1. 运行 -> cmd
wsl --status
  • 如果没有得到响应:
    1. 运行 -> SystemPropertiesAdvanced
    2. 性能 -> 设置 -> 高级选项卡 -> 高级选项卡 -> 虚拟内存 -> 更改
    3. 取消勾选 "自动管理所有驱动器的分页文件大小"
    4. 所有驱动器选择 "系统管理的大小"
    5. 保存后重启Windows
  1. 确保WSL版本为2, 否则Docker的守护线程将无法运行
wsl --set-default-verison 2
  1. 查看所有Linux发行版
wsl --list --online

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_7_9                        Oracle Linux 7.9
OracleLinux_8_7                        Oracle Linux 8.7
OracleLinux_9_1                        Oracle Linux 9.1
openSUSE-Leap-15.5                     openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-Server-15-SP5    SUSE Linux Enterprise Server 15 SP5
openSUSE-Tumbleweed                    openSUSE Tumbleweed
  1. 我们根据"NAME"安装第一个Ubuntu
wsl --install -d Ubuntu
  1. 查看所有发行版
wsl -l -v

  NAME      STATE           VERSION
* Ubuntu    Running         2
  1. 如果安装多个发行版, 可设置默认
wsl --set-default Ubuntu
  1. 打开Windows Termial, 点击最上方的下拉按钮, 选择我们刚才安装的Ubuntu, 初次进入需要设定用户名和密码
  2. 查看Ubuntu版本
lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy

安装Docker

  • 进入Linux环境后, 开始安装docker, docker官方已经整理好脚本, 我们用脚本安装就行
  1. 安装curl
sudo apt-get install -y curl
  1. 使用curl获取安装docker脚本, 等待脚本执行完成
curl -sSl https://get.docker.com/|sudo sh
  1. 查看docker版本:
sudo docker version
  1. 启动docker服务
sudo service docker start
  • 如果出现错误:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. 
Is the docker daemon running?

执行这个命令后, 选择1, 再次启动docker服务就可以了

sudo update-alternatives --config iptables
  1. 将当前用户加入docker用户组, 这样下面的命令就不用加sudo了
sudo groupadd docker
sudo gpasswd -a <用户名> docker
  1. 重启docker服务
sudo service docker restart

启动容器

  • 安装Redis镜像, 运行1个容器实例
  1. 拉取Redis镜像, 默认latest版本
docker pull redis
  1. 查看安装的所有镜像
docker images
  1. 运行Redis容器实例, 这里我们叫redis-1
  • 注意要通过-p参数将端口号6379暴露到外部的6389, 这样Windows进程才能访问到
docker run --name redis-1 -p 6389:6379 -d redis
  1. 查看所有容器, 包括已终止的
docker -a
  1. 在WSL内部访问Redis
docker exec -it redis-1 redis-cli
  1. 进入redis-cli后, 选择数据库0
select 0
  1. 插入一条数据
set mykey abc123
  1. 查看插入的值
get mykey

访问容器

  • 使用Spring Boot访问Redis
  1. pom中添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. application.properties添加redis的配置
  • 注意端口号是上文中我们暴露到外部的6389
spring.redis.host=127.0.0.1
spring.redis.port=6389
spring.redis.database=0
  1. 测试代码
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Object value = redisTemplate.opsForValue().get("mykey");
        System.out.println(value);
}

@Autowired
private  RedisTemplate redisTemplate;
  • 这里发现value竟然是null, 我们插入一条数据再看下
redisTemplate.opsForValue().set("mykey2", "qwer123");

插入这条数据后我们直接到WSL里的redis-cli查询所有的key

keys *

1) "\xac\xed\x00\x05t\x00\x06mykey2"
2) "mykey"

发现刚插入的mykey2前面多了一段乱码"\xac\xed\x00\x05t\x00\x06", 难怪get取不到值
我们可以指定RedisTemplate的泛型, 这样给到Redis的值就不会带有乱码了

@Autowired
private  RedisTemplate<String, String> redisTemplate;