说结论,再谈折腾过程。
结论:
Docker 在 Linux 系统下很好用,但并不适合在 Windows 系统下使用。想在 windows 下使用 docker ,可尝试使用 Linux 虚拟机安装运行 docker。
之前使用 Docker 容器一直是在 Linux 系统环境下,但平时开发都是 windows10 系统。
于是有个想法,想在 win10 下安装 docker 开发环境,来统一生产环境和开发环境。
折腾过程:
安装 Docker Machine 之前,要先安装 Docker for Windows。
安装 docker 引擎,得先开启 CPU虚拟化 支持和 Hyper-V 功能。
安装 docker machine 启动报错,提示 virtualbox 和 Hyper-V 不能共存。
$ docker-machine create --driver virtualbox default
Running pre-create checks...
Error with pre-create check: "This computer is running Hyper-V. VirtualBox won't boot a 64bits VM when Hyper-V is activated. Either use Hyper-V as a driver, or disable the Hyper-V hypervisor. (To skip this check, use --virtualbox-no-vtx-check)"
docker-machine 默认以 virtualbox 驱动模式,即 --driver virtualbox 创建 docker 虚拟机。
但64位的virtualbox虚拟机,在已开启 Hyper-V 功能的windows环境下,是无法启动的。
现在实际上有 2个方案
:
- 改用 Hyper-V 驱动运行 docker-machine 命令。(正确)
- 关闭 Hyper-V 功能,然后再正常启动 docker-machine。(错误)
网上的教程,基本上都第2种,关闭 Hyper-V ,需要重启电脑,耗时费力,搞到最后,连 docker 都启动不了。因为 windows 下的 Docker 引擎依赖 Hyper-V 功能。
关闭 Hyper-V 功能
控制面板--程序--程序和功能--启用和关闭 Windows 功能--取消勾选 Hyper-V 功能。
docker-machine create --driver virtualbox test
This computer doesn't have VT-X/AMD-v enabled
都说是 CPU虚拟化支持没开启,但确实已经打开了。网上说你的 Hyper-V 没有关闭成功,得使用 bcdedit 命令查看。还必须得 以管理员身份运行 CMD 命令才行。
bcdedit
......
hypervisorlaunchtype Auto
执行如下命令,关闭改选项,并重启电脑
bcdedit /set hypervisorlaunchtype off
https://blog.csdn.net/u012865169/article/details/103720545/
上面一波关闭 Hyper-V 操作之后,成功地导致了 Docker Engine 启动失败。提示如下:
Hardware assisted virtualization and data execution protection must be enabled in the BIOS. See https://docs.docker.com/docker-for-windows/troubleshoot/#virtualization
开启 Hyper-V 功能
控制面板--程序--程序和功能--启用和关闭 Windows 功能--勾选 Hyper-V 功能。
因为前面 bcdedit 命令把 hypervisorlaunchtype 项关闭了,必须执行命令手动开启,并重启电脑,Hyper-V 功能才能正常运行。以管理员身份运行如下命令:
bcdedit /set hypervisorlaunchtype auto
没想到百度到的技术文章,是这么不靠谱。后面自己看了下官方文档,其实是有说明的。
官方文档
Get started with Docker Machine and a local VM
IF YOU ARE USING DOCKER DESKTOP FOR WINDOWS
Docker Desktop for Windows uses Microsoft Hyper-V for virtualization,
and Hyper-V is not compatible with Oracle VirtualBox. Therefore, you
cannot run the two solutions simultaneously. But you can still use
docker-machine to create more local VMs by using the Microsoft Hyper-V
driver.
Windows 下的 Docker 桌面版,使用 Hyper-V 功能实现虚拟化,但Windows 下的 Hyper-V 功能与 Oracle公司的 VirtualBox 产品不兼容。因此,两种解决方案不可并存。
所以,只能使用 Hyper-V 方案,通过 docker-machine 命令来创建本地虚拟机。
The prerequisites are:
Have Docker Desktop for Windows installed, and running (which requires
that virtualization and Hyper-V are enabled, as described in What to
know before you install Docker Desktop for Windows).Set up the Hyper-V driver to use an external virtual network switch
See the Docker Machine driver for Microsoft Hyper-V topic, which
includes an example of how to do this.
开启虚拟化支持,创建虚拟交换机
使用 Hyper-V 方案创建 Docker 虚拟机之前,必须进行两项准备:
- 安装并运行 docker windows 桌面版(BIOS开启CPU虚拟化支持,开启 Hyper-V 功能)
- 通过
Hyper-V 管理器
,设置一个外部网络虚拟交换机
。
微软Hyper-V方案运行docker-machine https://docs.docker.com/machine/drivers/hyper-v/
https://docs.docker.com/machine/get-started/
$ docker-machine create --driver hyperv vm
Running pre-create checks...
Error with pre-create check: "Hyper-v commands have to be run as an Administrator"
必须以超级管理员身份运行,否则会有上面的提示。必须先通过 Hyper-V 管理器
创建一个 外部网络
虚拟交换机
,否则运行 docker-machine 会有如下报错:
docker-machine create --driver hyperv vm
Running pre-create checks...
Error with pre-create check: "no External vswitch found. A valid vswitch must be available for this command to run. Check https://docs.docker.com/machine/drivers/hyper-v/"
按官网说明,一切准备就绪。但还是报错,因为天朝网络太卡了,初次使用时,下载 Boot2Docker IOS 文件失败。
docker-machine create --driver hyperv vm
Running pre-create checks...
(vm) No default Boot2Docker ISO found locally, downloading the latest release...
Error with pre-create check: "Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: read tcp 192.168.3.8:52998->192.30.255.117:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
手动下载 boot2docker.ios 文件:
https://github.com/boot2docker/boot2docker/releases/download/v18.09.8/boot2docker.iso
并将此文件拷贝到此路径下面:
C:\Users\xxx\.docker\machine\cache\
https://blog.csdn.net/gongli109/article/details/105618632/
下面 docker-machine 终于可以成功运行了。
创建docker虚拟机:
docker-machine create --driver hyperv vm
Running pre-create checks...
(vm) Unable to get the latest Boot2Docker ISO release version: Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: EOF
Creating machine...
(vm) Unable to get the latest Boot2Docker ISO release version: Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: dial tcp 192.30.255.116:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
(vm) Copying C:\Users\yourname\.docker\machine\cache\boot2docker.iso to C:\Users\yourname\.docker\machine\machines\vm\boot2docker.iso...
(vm) Creating SSH key...
(vm) Creating VM...
(vm) Using switch "Primary Virtual Switch"
(vm) Creating VHD
(vm) Starting VM...
(vm) Waiting for host to start...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env vm
查看虚拟机环境变量:
docker-machine env vm
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.3.19:2376
SET DOCKER_CERT_PATH=C:\Users\yourname\.docker\machine\machines\vm
SET DOCKER_MACHINE_NAME=vm
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM @FOR /f "tokens=*" %i IN ('docker-machine env vm') DO @%i
挂载虚拟机远程文件夹到本地目录:
mkdir foo
docker-machine ssh vm mkdir foo
docker-machine mount vm:/home/docker/foo foo
You must have a copy of the sshfs binary locally to use the mount feature
上面报错了,因为挂载功能,需要用到一个 sshfs
的服务。在Windows下安装不成熟的 sshfs
服务,个人不大推荐。
Windows 10 开启 NFS 客户端
功能,是比较推荐的方案,因为win10企业版和专业版,都带有此项功能,但默认不开启。
挂载远程目录到本地X盘
mount 192.168.1.101:/es X:
Windows10配置NFS服务端和客户端 https://www.cnblogs.com/xuxy03/p/6232378.html
windows下的SSHFS
首先在GitHub上下载DokanSetup-1.0.5.1000和WinSSHFS-1.6.1.13-devel
注意:Dokan不能使用最新的版本,得使用1.0.5版本。要不win-sshfs会报Dokan版本错误的问题。(win10版本)
按照提示,安装Dokan和win-sshfs。并安装提示安装的依赖包Visual C++ Redistributable for Visual Studio 2015(x86版本)。
https://blog.csdn.net/chenyangah/article/details/82957279