Windows11部署Docker

https://docs.docker.com/desktop/windows/install/

https://docs.microsoft.com/en-us/windows/wsl/basic-commands

一、下载Docker for Windows

到Docker官网下载 Docker for Windows,并安装

二、安装WSL2

2.1、启动WSL

以管理员身份在PowerShell中运行以下命令

1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

2.2、启动虚拟机功能

以管理员身份在PowerShell中运行以下命令,运行后需重启计算机

1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

2.3、安装Linux内核更新包

下载地址: wsl_update_x64.msi

2.4、设置WSL2为默认版本

以管理员身份在PowerShell中运行以下命令

1
wsl --set-default-version 2

三、Docker并迁移镜像到非系统盘

在Windows11中默认已安装Windows Terminal 推荐使用其操作Docker,打开Windows Terminal 输入命令 wsl -l --all -v 来查看Docker的运行状态

执行命令之后可以看到如上图所示Docker的运行状态,Running表示正在运行中,使用wsl --shutdown 关闭运行中的Docker

再次运行命令可以查看到Docker已处于停止状态,此时就可以进入镜像迁移的步骤。

导出Docker镜像

1
2
3
4
# C:\Users\<user>\AppData\Local\Docker\wsl
wsl --export docker-desktop-data "D:\docker\docker-desktop-data.tar"
wsl --export docker-desktop "D:\docker\docker-desktop.tar"
wsl --export Ubuntu-20.04 "D:\docker\Ubuntu-20.04.tar"

路径为docker导出文件存储的位置

注销镜像

1
2
3
wsl --unregister docker-desktop
wsl --unregister docker-desktop-data
wsl --unregister Ubuntu-20.04

导入之前导出的镜像

1
2
3
wsl --import docker-desktop d:\docker\docker-desktop d:\docker\docker-desktop.tar
wsl --import docker-desktop-data d:\docker\docker-desktop-data d:\docker\docker-desktop-data.tar
wsl --import Ubuntu-20.04 d:\docker\Ubuntu-20.04 d:\docker\Ubuntu-20.04.tar

重启Docker

四、安装Linux分发版

直接选了评分最多的Ubuntu,按下Windows 键和R,在对话框中输入wsreset,点击确定,等待程序运行完毕自动打开应用商店。也可以通过wsl -install安装

五、配置国内镜像仓库

3.1、右键托盘图标 - 设置

3.2、修改Docker Engine配置,增加镜像仓库地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": [],
"debug": false,
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"features": {
"buildkit": true
}
}

科大镜像:https://docker.mirrors.ustc.edu.cn/

网易:https://hub-mirror.c.163.com/

阿里云:https://<你的ID>.mirror.aliyuncs.com

七牛云加速器:https://reg-mirror.qiniu.com

六、WSL2配置文件(需要重启)

https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig

mv wsl.conf \\wsl.localhost\Ubuntu\etc\wsl.conf

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
31
32
33
34
35
# wsl.conf

# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c.
root = /

# DrvFs-specific options can be specified.
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
#hostname = DemoHost
#generateHosts = false
#generateResolvConf = false

# Set whether WSL supports interop process like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = false
appendWindowsPath = false

# Set the user when launching a distribution with WSL.
#[user]
#default = DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command = service docker start

https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-global-options-with-wslconfig

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
31
32
33
34
35
# C:\Users\<user>\.wslconfig


# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB

# Sets the VM to use two virtual processors
processors=2

# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
#kernel=C:\\temp\\myCustomKernel

# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
#kernelCommandLine = vsyscall=emulate

# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB

# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
#swapfile=C:\\temp\\wsl-swap.vhdx

# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=false

# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true

# Disables nested virtualization
nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
debugConsole=true

重启docker desktop

七、docker部署的SqlServer过几秒自动停止

1
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Aa123456!@#" -p 1401:1433 -d mcr.microsoft.com/mssql/server:latest

其中ACCEPT_EULA=Y的意思是同意许可协议,必选MSSQL_SA_PASSWORD为sa用户的密码,要求最少8位的强密码,要有大写字母,小写字母,数字以及特殊符号,否则会有一个大坑(docker启动sqlserver容器后过几秒就停止了,报EXITED(1)的错)

前面的1401端口表示映射到主机的1401端口

后面的1433端口表示容器中的sqlserver服务用到的1433端口

八、启动本k8s

https://docs.docker.com/desktop/kubernetes/#disable-kubernetes

1
2
kubectl config get-contexts
kubectl config use-context docker-desktop

kubernetes一直处于starting状态解决方法

https://blog.csdn.net/gzseehope/article/details/108881193

https://github.com/AliyunContainerService/k8s-for-docker-desktop/tree/v1.22.5

解决Windows上Docker Desktop中Kubernetes一直处于starting的问题

在 Docker Desktop 中启用 K8s 服务

如何成功启动 Docker 自带的 Kubernetes?(2022年1月更新)

Windows 下 DockerCE 启用 K8s 报错 kubernetes.docker.internal no such host

参考

看看Docker Desktop WSL2 backend

Win11正式版升级安装Docker(基于WSL2)

Windows11安装Docker并迁移镜像到非系统盘

手把手教你踩坑:老白的Docker for Windows安装初探WSL 2 backend

问题

日志信息

1
2
3
github.com/moby/vpnkit/go/cmd/kube-vpnkit-forwarder/main.go:49: Failed to list *v1.Service: services is forbidden: User "system:serviceaccount:kube-system:vpnkit-controller" cannot list resource "services" in API group "" at the cluster scope

log: exiting because of error: log: cannot create log: open /tmp/kube-vpnkit-forwarder.vpnkit-controller.unknownuser.log.ERROR.20210815-072638.1: no such file or directory

网上查找半天说是在 C:\Windows\System32\drivers\etc\hosts 文件添加配置,重启 DockerDesktop 即可。

Resolve-DnsName kubernetes.docker.internal

1
127.0.0.1 kubernetes.docker.internal

重启后问题成功解决。

我们来查看下集群状态 (搞错了。。。这个是集群启动失败的情况)

1
2
3
4
kubectl cluster-info

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Unable to connect to the server: dial tcp: lookup kubernetes.docker.internal: no such host

集群启动成功的正确姿势

1
2
3
4
5
6
kubectl cluster-info

Kubernetes control plane is running at https://kubernetes.docker.internal:6443
CoreDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

到此,使用 DockerDesktop 运行 Kubernetes 已经成功。

在macOS上面,执行 rm -fr '~/Library/Group\ Containers/group.com.docker/pki'

在Windows上面删除 'C:\ProgramData\DockerDesktop\pki' 目录 和 'C:\Users\yourUserName\AppData\Local\Docker\pki' 目录