=========================

第一大步骤:检查wsl本身,并更新到wsl2去



结果我发现我还是1???


PS C:\Users\Administrator> wsl -l -v

  NAME      STATE           VERSION

* Ubuntu    Running         1

PS C:\Users\Administrator>

PS C:\Users\Administrator>

PS C:\Users\Administrator>

PS C:\Users\Administrator>

PS C:\Users\Administrator> wsl --set-default-version 2

请启用虚拟机平台 Windows 功能并确保在 BIOS 中启用虚拟化。

有关信息,请访问 https://aka.ms/enablevirtualization

PS C:\Users\Administrator> wsl -l -v

  NAME      STATE           VERSION

* Ubuntu    Stopped         1

PS C:\Users\Administrator> wsl --set-version Ubuntu 2

正在进行转换,这可能需要几分钟时间...

有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2

WSL 2 需要更新其内核组件。有关信息,请访问 https://aka.ms/wsl2kernel

PS C:\Users\Administrator>


切一下版本哈


===========================


https://blog.csdn.net/ls0111/article/details/128513930


WSL2支持systemctl命令


systemctl list-unit-files --type=service


sudo systemctl


不再报错了不是


lemonhall@HOIN-XX-YuNing:~$ sudo systemctl enable docker

Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.

Executing: /lib/systemd/systemd-sysv-install enable docker

lemonhall@HOIN-XX-YuNing:~$


lemonhall@HOIN-XX-YuNing:~$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

719385e32844: Pull complete

Digest: sha256:88ec0acaa3ec199d3b7eaf73588f4518c25f9d34f58ce9a0df68429c5af48e8d

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/


lemonhall@HOIN-XX-YuNing:~$



然后RUN一个docker的hello-world看看,OK,也是OK的



===========================


wsl2要启动是有依赖的,首先需要在windows功能里启动


【虚拟机平台】这个玩意儿



PS C:\Users\Administrator> wsl --set-default-version 2

请启用虚拟机平台 Windows 功能并确保在 BIOS 中启用虚拟化。

有关信息,请访问 https://aka.ms/enablevirtualization

PS C:\Users\Administrator>


然后windwos自己会开始下载对应的组件,最后还需要在BIOS里面开启虚拟化的功能


wsl --set-version Ubuntu 2


这样可以切已有版本到2去


PS C:\Users\Administrator> wsl -l -v

  NAME      STATE           VERSION

* Ubuntu    Stopped         1

PS C:\Users\Administrator>


这样可以看当前的版本


PS C:\Users\Administrator> wsl --set-version Ubuntu 2

正在进行转换,这可能需要几分钟时间...

有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2

WSL 2 需要更新其内核组件。有关信息,请访问 https://aka.ms/wsl2kernel

PS C:\Users\Administrator>


PS C:\Users\Administrator> wsl --set-default-version 2

有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2

操作成功完成。

PS C:\Users\Administrator>


我开启了BIOS里的虚拟化,开启了服务里的虚拟机和虚拟机平台以及linux子系统三个服务,然后就能切换默认的到2就成功了


https://learn.microsoft.com/zh-cn/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package



所以按照提示的意思,我几次切换版本都不成功的 主要原因是没安装一个更新的组件,好吧,简直是在猜对方的意思


到微软上面的那个地址上面去,下载


wsl_update_x64.msi,一个几十兆的包而已,还好,下载下来更新


啊,好像更靠谱的方式是:

wsl.exe --update


更新一下整个子系统


PS C:\Users\Administrator> wsl --set-version Ubuntu 2

有关与 WSL 2 关键区别的信息,请访问 https://aka.ms/wsl2


正在进行转换,这可能需要几分钟时间。..



又运行了一下命令,我靠,这次对了,带着等待进度条


wsl -l -v


PS C:\Users\Administrator> wsl -l -v

  NAME      STATE           VERSION

* Ubuntu    Stopped         2

PS C:\Users\Administrator>


OK,这次转换成功了


https://blog.csdn.net/ls0111/article/details/128513930


回到CSDN的文章上面去


在子系统内部检查一下配置:

lemonhall@HOIN-XX-YuNing:~$ cat /etc/wsl.conf

[boot]

systemd=true

lemonhall@HOIN-XX-YuNing:~$






================================================

================================================

第二大步骤:安装docker本身



报错: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.


export DOCKER HOST=unix:///var/run/docker.sock



https://www.cnblogs.com/zhyp/p/17589396.html


一、安装依赖:

sudo apt-get update

sudo apt-get install ca-certificates curl gnupg


========================


二、添加 Docker 的官方 GPG 密钥:

sudo install -m 0755 -d /etc/apt/keyrings

curl -fsSI https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

sudo chmod a+r /etc/apt/keyrings/docker.gpg


========================


三、配置出来一个存储库:


echo \

"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \

"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \

sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


然后检查一下文件:

deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable


嗯,就是这么一行内容


=========================



四、更新存储库、并安装

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli

sudo apt-get install containerd.io docker-buildx-plugin docker-compose-plugin


=========================

https://blog.csdn.net/weixin_44246257/article/details/132901677


五、修改iptables工作模式

此时不能直接启动docker,Ubuntu22.04使用了iptables-nft,而WSL2不支持,可以通过以下命令进行修改

sudo update-alternatives --config iptables


选1


=========================


修改后,再启动docker,WSL2经过测试无法使用 systemctl 命令,因此我们使用 service 命令启动Docker daemon。命令如下所示:

sudo service docker start


启动之后,再执行以下命令进行验证

sudo service docker status


若看到以下信息,说明docker启动成功


* Docker is running



================================================

================================================

第三步:安装code-server本身




执行一下脚本来启动一个code-server


#!/bin/bash


#mkdir.config

sudo docker run -it --name code-server-shr -p 8023:8080 \

-v "/home/lemonhall/vscode/.config:/home/coder/.config" \

-v "/home/lemonhall/project/:/home/coder/project" \

-u "$(id -u):$(id -g)" \

-e "DOCKER USER=$USER" \

codercom/code-server:latest




先在当前文件夹下新建config 文件夹,如果里面,文件夹结构为: config/code-server/config.yaml文件内容基本如下。


bind-addr: 本机ip地址:和 docker 映射的本机端口号,例: 8023(和上面保持一致)(127...0:8023)

auth: password

password: 设置相应密码

cert: false



实际上是:/home/lemonhall/vscode/.config/code-server,建一个新文件,config.yaml

bind-addr: 0.0.0.0:8023

auth: password

password: OPyK4&wR0w^h

cert: false


-v "project/:/home/coder/project" 根据自己需要映射

-v "env/:/home/coder/env" 根据自己需要映射,这里我映射的是本机 python 环境地址



lemonhall@HOIN-XX-YuNing:~$ sudo docker run -it --name code-server-shr -p 8023:8080 \

        -v "/home/lemonhall/vscode/.config:/home/coder/.config" \

        -v "/home/lemonhall/project/:/home/coder/project"       \

        -u "$(id -u):$(id -g)" \

        -e "DOCKER USER=$USER" \

        codercom/code-server:latest

[2023-10-31T03:55:22.350Z] info  code-server 4.18.0 d7a2b4936af1bfd80cb96f2567af68badc2325e3

[2023-10-31T03:55:22.352Z] info  Using user-data-dir /home/coder/.local/share/code-server

[2023-10-31T03:55:22.376Z] info  Using config file /home/coder/.config/code-server/config.yaml

[2023-10-31T03:55:22.376Z] info  HTTP server listening on http://0.0.0.0:8080/

[2023-10-31T03:55:22.376Z] info    - Authentication is enabled

[2023-10-31T03:55:22.376Z] info      - Using password from /home/coder/.config/code-server/config.yaml

[2023-10-31T03:55:22.376Z] info    - Not serving HTTPS

[2023-10-31T03:55:22.377Z] info  Session server listening on /home/coder/.local/share/code-server/code-server-ipc.sock



这样会启动起来一个交互式的console,倒是可以用来检查一下哈


然后我发现host的windwos机器,或直接自动映射这个8023地址,这一点挺好,不用我麻烦了


http://localhost:8023/login



sudo docker run -itd --name code-server-shr -p 8023:8080 \

-v "/home/lemonhall/vscode/.config:/home/coder/.config" \

-v "/home/lemonhall/project/:/home/coder/project" \

-u "$(id -u):$(id -g)" \

-e "DOCKER USER=$USER" \

codercom/code-server:latest