在WSL(Windows Subsystem for Linux)的linux(ubuntu)上运行docker

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37422289/article/details/80719836

主要解决问题:

docker守护进程 docker Daemon在WSL缺少支持的机制,无法启动服务.

执行

docker version

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

解决方法:

在windows10安装Docker-Toolbox启动docker服务, WSL通过socket连接localhost,使用docker.
因为使用win10家庭版,缺少hyper-V,无法安装Docker for Windows Installer.根据官网给出的解决方案, 安装docker-toolbox绕过hyper-V.

If your system does not meet the requirements to run Docker for Windows, you can install Docker Toolbox, which uses Oracle Virtual Box instead of Hyper-V.

背景知识:

Docker使用client/server的方式运行,客户端docker通过命令行或HTTP的REST API方式向服务端运行的守护进程docker daemon请求服务. docker daemon默认运行在本地,也可运行在远程服务器,通过指定ip地址和端口建立连接.

解决步骤

完成ubuntu上docker的安装:

安装教程:Get Docker CE for Ubuntu
安装完成后,正常情况下docker daemon会自动作为service运行.

也可手动执行

sudo service docker start

或者

sudo service docker restart

输出:

Starting Docker: docker     [ OK ]                                               

运行:

docker version

客户端服务端(目前都在本机上)运行成功的话,可以正常打印出客户端服务端版本信息. 对于WSL来说,docker daemon启动失败.version指令只能打印出client的版本信息,而访问server出错,熟悉的错误信息.
输出:

Client:
Version:      18.03.1-ce
API version:  1.37
Go version:   go1.9.5
Git commit:   9ee9f40
Built:        Thu Apr 26 07:17:20 2018
OS/Arch:      linux/amd64
Experimental: false
Orchestrator: swarm
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

因为底层原因,WSL无法运行docker daemon是无解的.

替代方案有:
1) 转到windows平台完成docker部署;
2) 本地安装真正的linux系统完成部署,或购买云服务器完成部署(除了花点钱,轻松愉快)
3) 在windows启动docker daemon也就是docker server, WSL上通过socker连接,实现服务.

我们以第三种方案实现:

完成windows上docker的安装:

如果是windows 10专业版\教育版\企业版,可直接根据Install Docker for Windows 完成安装;
如果不是以上版本,运行Docker for Windows Installer出现
Installation fail: one pre-requisite is not fullfilled.
dockerfail

则使用Docker-toolbox完成安装: Docker-Toolbox下载地址

安装完成开始菜单中多了两个程序:
1) Docker Quickstart Terminal:docker 命令行工具
在该终端中执行:

docker version

可以看到正确打印出了client和server端的docker版本信息.(which was supposed to work correctly in linux.)

2) Kitematic: 管理docker各种image和container的GUI可视化界面工具.
该界面非常友好,左侧已经有很多分享的经典image供选择.
打开一个hello-world的image即可创建container并运行(在本机上初次运行时间较长,大概等待十秒)

到这里,如果只是为了使用docker功能,那么在docker-toolbox里面就可以满足你的需求.尝试在Kitematic中选择nginx的image并启动容器,查看demo网页.一个nginx的部署动动手指头就完成了.有过nginx部署经验的朋友可以初次感受到docker带来的便利所在.

在WSL上连接windows的Docker Daemon

相信大多数朋友使用WSL并不是为了实际开发,而是为了熟悉linux 操作.既然如此,让docker在WSL上真正跑起来,完全是有意义的.

  1. 在已经安装好docker的windows上打开cmd命令行,执行docker-machine ls可以看到当前本机运行的docker daemon.URL就是进程运行的地址.
    cmd
  2. 在WSL的ubuntu上执行

    docker -H 192.168.99.100:2376
    

    提示Are you trying to connect to a TLS-enabled daemon without TLS?
    显然需要配置证书,可以在config配置文件当中设置免TLS连接,但是毕竟不是安全的做法.关于TLS的原理可以查看:TLS整理(下):TLS如何保证安全

  3. 不管你看了原理还是没看,下一步应该在客户端,也就是WSL的linux上配置证书与密钥.
    在windows的cmd中执行:

    docker-machine regenerate-certs --client-certs default
    

    注: default是我本机的docker server的名称. 可对应上图中的NAME修改.
    执行完毕,生成密钥文件ca.pem,cert.pem,key.pem
    在你的windows的
    C:\Users\<windows系统登录用户名>\.docker\machine\machines\default(对应NAME)文件夹当中.

  4. 在WSL当中访问/mnt/c/ 然后得到上述 ca.pem,cert.pem,key.pem三个文件,复制到/home/你的ubuntu用户文件夹/.docker 当中(比如我的是/home/chrisyang/.docker/).如不清楚目标文件夹,在WSL中执行docker即可看到.
    tls

  5. 在WSL中运行

    docker -H 192.168.99.100:2376 --tlsverify version
    

成功打印出client和server的版本信息,连接完成.

执行

docker -H 192.168.99.100:2376 --tlsverify run hello-world

也得到正确输出.

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/engine/userguide/

猜你喜欢

转载自blog.csdn.net/m0_37422289/article/details/80719836