Windows 10 及 WSL 上安装 docker

Windows 版 Docker 下载安装

注意 docker 分两个版本 CE(Community Edition) 和 EE(Enterprise Edition)。EE 面向企业,更稳定并提供一定的维护期,CE 版本领先,拥有更多新特性,一般开发使用 CE 版,下载地址:

https://store.docker.com/editions/community/docker-ce-desktop-windows

双击安装,当 docker 的鲸标出现在系统托盘处时就标志安装成功了。

注:需要开启 hyper-v
方法:控制面板 -> 程序 -> 启用或关闭Windows功能 -> 选中Hyper-V

执行验证

C:\Users\58>docker version
Client:
 Version:      17.09.0-ce
 API version:  1.32
 ........

C:\Users\58>docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
.........

至此宿主机安装就算完成了

Windows Subsystem for Linux 上安装

正常情况下通过 apt-get 安装完 docker 后运行会报

lzxz1234@58-PC:/mnt/c/Users/58$ docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

因为 WSL 本身是不支持安装 docker engine 的,所以想在 WSL 系统中运行 docker 命令可以修改客户端的设置,需要将其直接连接宿主机的 docker engine。

  1. 首先需要开启宿主机的监听,从托盘入找到 docker,右击 setting -> General 选中 Expose daemon on tcp://localhost:2375 without TLS
  2. 将 export DOCKER_HOST=127.0.0.1:2375 添加到 ~/.profile 并执行 source ~/.profile
  3. 尝试 docker run hello-world,现在就会发现执行正常了

All Done !

完美收工

记一个错误

如果 hyper-v 启动失败时就会出现以下错误:

Hardware assisted virtualization and data execution protection must be endbled in the BIOS. See https://docs.docker.com/docker-for-windows/troubleshoot/#virtualization-must-be-enabled

这个的意思是虚拟化未开启,一般虚拟化需要通过 BIOS 开启,通过任务管理器可以查看当前状态:

image

如果虚拟化开启的时候仍报错,可以尝试从控制面板关闭 hyper-v 后重新开启尝试一下。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注