当前位置:   article > 正文

史上最全从0开始教你玩转wsl2+docker,构建自己的开发环境_wsl2 docker

wsl2 docker

1、安装wsl

需要windows版本大于

d7bf687a3c3a654b7fee10568ea538d8.png

搜索启用或关闭windows功能

36ad77bb8c9f4314b650d49d0c80b8ed.png

把图片中红点标注的功能勾选,注意勾选hyper-v就不能使用虚拟机类软件,如vm,安卓模拟器一类,点击确定,重启电脑。

打开任务管理器

70315604273b452584e084dd4dd01f27.png

确保虚拟化已经启用,部分设备可能需要去bios设置,自行查阅下相关资料

下载64位wsl2内核升级包    https%3A//wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

安装后,设置默认wsl版本

wsl --set-default-version 2

安装linux系统

方法一:可以从微软商店搜索安装,有概率会失败

方法二: 翻到页面最下方,下载你需要的版本    旧版 WSL 的手动安装步骤 | Microsoft Learn在旧版 Windows 上手动(而不是使用 wsl 安装命令)安装 WSL 的分步说明。icon-default.png?t=N7T8https://docs.microsoft.com/zh-cn/windows/wsl/install-manual给下载到的文件添加一个.zip后缀,用解压缩软件解压到你想安装的地方

e4d4b9658f4b42bb8752ca84d75377e4.png

点击里面的ubnutu.exe文件,等待一会就会安装成功

去cmd中输入 wsl -l -v 查看安装的子系统版本

2、安装完成后的设置:

安装完成后会设置自己的用户名,密码(注意不是root账户)

设置root密码

sudo passwd

切换root用户测试

su root

每次切换root用户很麻烦,可以用powershell执行下面这条命令,默认root用户登录,其中参数自己替换下

C:\Users\用户名\AppData\Local\Microsoft\WindowsApps\ubuntu版本.exe config --default-user root

开启ssh服务

首先确保是root用户

vi /etc/ssh/sshd_config

进入之后,修改如下配置,可以直接复制我的,(我修改的项加了注释)

  1. # $OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 djm Exp $
  2. # This is the sshd server system-wide configuration file. See
  3. # sshd_config(5) for more information.
  4. # This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin
  5. # The strategy used for options in the default sshd_config shipped with
  6. # OpenSSH is to specify options with their default value where
  7. # possible, but leave them commented. Uncommented options override the
  8. # default value.
  9. Port 22 #22端口,ssh服务默认端口
  10. #AddressFamily any
  11. ListenAddress 0.0.0.0 #所有ip都可以联通
  12. #ListenAddress ::
  13. #HostKey /etc/ssh/ssh_host_rsa_key
  14. #HostKey /etc/ssh/ssh_host_ecdsa_key
  15. #HostKey /etc/ssh/ssh_host_ed25519_key
  16. # Ciphers and keying
  17. #RekeyLimit default none
  18. # Logging
  19. #SyslogFacility AUTH
  20. #LogLevel INFO
  21. # Authentication:
  22. #LoginGraceTime 2m
  23. #PermitRootLogin prohibit-password
  24. #StrictModes yes
  25. #MaxAuthTries 6
  26. #MaxSessions 10
  27. PermitRootLogin yes #把上面那行相同的注释掉,新加一行,允许root账户登录
  28. #PubkeyAuthentication yes
  29. # Expect .ssh/authorized_keys2 to be disregarded by default in future.
  30. #AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
  31. #AuthorizedPrincipalsFile none
  32. #AuthorizedKeysCommand none
  33. #AuthorizedKeysCommandUser nobody
  34. # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
  35. #HostbasedAuthentication no
  36. # Change to yes if you don't trust ~/.ssh/known_hosts for
  37. # HostbasedAuthentication
  38. #IgnoreUserKnownHosts no
  39. # Don't read the user's ~/.rhosts and ~/.shosts files
  40. #IgnoreRhosts yes
  41. # To disable tunneled clear text passwords, change to no here!
  42. PasswordAuthentication yes #允许使用密码登录
  43. #PermitEmptyPasswords no
  44. # Change to yes to enable challenge-response passwords (beware issues with
  45. # some PAM modules and threads)
  46. ChallengeResponseAuthentication no
  47. # Kerberos options
  48. #KerberosAuthentication no
  49. #KerberosOrLocalPasswd yes
  50. #KerberosTicketCleanup yes
  51. #KerberosGetAFSToken no
  52. # GSSAPI options
  53. #GSSAPIAuthentication no
  54. #GSSAPICleanupCredentials yes
  55. #GSSAPIStrictAcceptorCheck yes
  56. #GSSAPIKeyExchange no
  57. # Set this to 'yes' to enable PAM authentication, account processing,
  58. # and session processing. If this is enabled, PAM authentication will
  59. # be allowed through the ChallengeResponseAuthentication and
  60. # PasswordAuthentication. Depending on your PAM configuration,
  61. # PAM authentication via ChallengeResponseAuthentication may bypass
  62. # the setting of "PermitRootLogin without-password".
  63. # If you just want the PAM account and session checks to run without
  64. # PAM authentication, then enable this but set PasswordAuthentication
  65. # and ChallengeResponseAuthentication to 'no'.
  66. UsePAM yes
  67. #AllowAgentForwarding yes
  68. #AllowTcpForwarding yes
  69. #GatewayPorts no
  70. X11Forwarding yes
  71. #X11DisplayOffset 10
  72. #X11UseLocalhost yes
  73. #PermitTTY yes
  74. PrintMotd no
  75. #PrintLastLog yes
  76. #TCPKeepAlive yes
  77. #UseLogin no
  78. #PermitUserEnvironment no
  79. #Compression delayed
  80. #ClientAliveInterval 0
  81. #ClientAliveCountMax 3
  82. #UseDNS no
  83. #PidFile /var/run/sshd.pid
  84. #MaxStartups 10:30:100
  85. #PermitTunnel no
  86. #ChrootDirectory none
  87. #VersionAddendum none
  88. # no default banner path
  89. #Banner none
  90. # Allow client to pass locale environment variables
  91. AcceptEnv LANG LC_*
  92. # override default of no subsystems
  93. Subsystem sftp /usr/lib/openssh/sftp-server
  94. # Example of overriding settings on a per-user basis
  95. #Match User anoncvs
  96. # X11Forwarding no
  97. # AllowTcpForwarding no
  98. # PermitTTY no
  99. # ForceCommand cvs server

重启ssh项目

sudo service ssh restart

使用ssh工具测试链接

20ee67686376478fad02905b22ab773f.png

成功

3、windows下docker-desktop的安装

官网下载docker-desktop的安装包,一定要确保第一步开启的windows功能都开启了

Download Docker Desktop | Docker   

正常软件安装流程

进入软件,点击小齿轮进入设置

7eb77bd241f1493489c332a98338c274.png

勾选你安装的ubnutu

 cdc0ac2185c94feeb72548e0fb98572a.png

 更改镜像仓库地址,我使用的阿里云,网上也有好多,比如中科大等

  1. {
  2. "debug": true,
  3. "experimental": false,
  4. "registry-mirrors": [
  5. "https://lcuu39wt.mirror.aliyuncs.com"
  6. ]
  7. }

resource中可以更改docker镜像位置,推荐更改,不然c盘会占用很高

设置完之后,打开终端,输入docker ps

9ca5bbdaf547478cbad6b2c818fedb8f.png

 就会发现docker和你的linux子系统已经连接起来了

4、配置wsl2新特性,一定要开,否则会出现内存泄漏,爆内存的情况

C:\Users\电脑用户名文件夹下新建一个.wslconfig文件

  1. # Settings apply across all Linux distros running on WSL 2
  2. [wsl2]
  3. # Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
  4. memory=8GB
  5. # Sets the VM to use two virtual processors
  6. processors=8
  7. [experimental]
  8. autoMemoryReclaim=gradual # 开启自动回收内存,可在 gradual, dropcache, disabled 之间选择
  9. networkingMode=mirrored # 开启镜像网络
  10. dnsTunneling=true # 开启 DNS Tunneling
  11. firewall=true # 开启 Windows 防火墙
  12. autoProxy=true # 开启自动同步代理
  13. sparseVhd=true # 开启自动释放 WSL2 虚拟硬盘空间

重启wsl2或重启电脑生效

5、存在的问题及解决思路

ssh服务不会自动开启

解决思路:

重启ssh服务的命令,根据需要修改参数哦,可以写个cmd脚本放在windows自启动文件夹中,开机自己运行就不用每次输入了

C:\Users\用户名\AppData\Local\Microsoft\WindowsApps\ubuntu2004.exe run "sudo service ssh restart"

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/385306
推荐阅读
相关标签
  

闽ICP备14008679号