Win10内置Ubuntu,完美使用Docker in Windows

17 篇文章 3 订阅 ¥19.90 ¥99.00

关键字:Win10 Ubuntu18 WSL Docker “remote Docker daemon”

  • 问题:
    • WSL如何完美配合Docker使用呢?难道一定要通过VirtualBox么?或者一定要安装单独的Ubuntu系统么?
    • 答案:很简单,通过remote Docker daemon连接!
  • 优点:
    • Docker基本无性能损失
    • 无须另外安装VirtualBox等虚拟机
    • 方便同时使用Win10、Ubuntu两个系统

image.png

框架

Win10 + Docker <-- 本机共享文件夹Volume --> Ubuntu命令行(in WSL)
Ubuntu --> remote Docker daemon --> Docker in Windows
Win10浏览器 --> Docker in Windows --> Ubuntu IP --> Ubuntu内服务

步骤

1. 安装Docker in Windows10

2. 在Win10 WSL里安装Ubuntu

  • 这教程太多了,自行搜索。大致是打开虚拟化HyperV,安装Windows Feature: WSL,重启,微软商店里安装“Ubuntu18.04”

     

    image.png

  • 安装完毕,通过开始菜单 - Ubuntu打开,设置Linux用户名/密码
  • Win+R - “Bash”也能打开,注意跟上一步起始目录的区别

3. Ubuntu18安装Docker CE

 

# Allow your user to access the Docker CLI without needing root access.
sudo usermod -aG docker $USER
  • 安装Docker Compose

 

# Install Python and PIP.
sudo apt-get install -y python3 python3-pip

# Install Docker Compose into your user's home directory.
pip install --user docker-compose

4. 连接Docker daemon

  • 最关键的一行命令,打开Bash:

 

echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc

不通过deamon连接的话,你在Ubuntu里运行docker,就会出现错误:

 

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
  • 验证Docker启动成功:

 

docker info
docker-compose --version

 

kevinqq@CN:/mnt/c/Users/xxx$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

至此,已经在WSL Ubuntu里完美配置Docker成功了!

5. 进阶:使用Docker开发时要注意Win10共享文件夹的问题

Docker in Window里,只允许访问c:\Users\<id>\目录下的东西,Win+R - Bash打开时,默认目录也是这个文件夹

WSL Ubuntu18里,默认映射Win C盘为/mnt/c/,需要手动改为/c/

 

# Running Windows 10 17.09?
sudo mkdir /c
sudo mount --bind /mnt/c /c

测试一下docker-compose一个Flask网站:
https://docs.docker.com/compose/gettingstarted/

 

kevinqq@CN-00009841:/c/Users/xxx/git$ git clone https://github.com/kevinqqnj/docker-compose-starter.git
kevinqq@CN-00009841:/c/Users/xxx/git$ cd docker-compose-starter 
kevinqq@CN-00009841:/c/Users/xxx/git/docker-compose-starter$ docker-compose up --build
Building web
Step 1/5 : FROM python:3.6-alpine
 ---> 1837080c5e87
Step 2/5 : ADD . /code
 ---> Using cache
 ---> 1c28f8605b6f
Step 3/5 : WORKDIR /code
 ---> Using cache
 ---> 3b36663d505b
Step 4/5 : RUN pip install -r requirements.txt
 ---> Using cache
 ---> 5af3cbe591d4
Step 5/5 : CMD ["python", "app.py"]
 ---> Using cache
 ---> cf356692af43
Successfully built cf356692af43
Successfully tagged docker-compose-starter_web:latest
Starting docker-compose-starter_redis_1 ... done
Starting docker-compose-starter_web_1   ... done
Attaching to docker-compose-starter_web_1, docker-compose-starter_redis_1
...
redis_1  | 1:M 13 Jan 2019 07:45:08.470 * Ready to accept connections
web_1    |  * Serving Flask app "app" (lazy loading)
web_1    |  * Environment: production
web_1    |    WARNING: Do not use the development server in a production environment.
web_1    |    Use a production WSGI server instead.
web_1    |  * Debug mode: on
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

此时,在Ubuntun里命令行访问,已经OK了:

 

kevinqq@CN-00009841:/c/Users/xxx$ curl http://localhost:5000
Hello World! I have been seen 1 times.
kevinqq@CN-00009841:/c/Users/xxx$ curl http://localhost:5000
Hello World! I have been seen 2 times.
kevinqq@CN-00009841:/c/Users/xxx$ curl http://localhost:5000
Hello World! I have been seen 3 times.

在Win10里访问localhost是不行的,需要先查看Ubuntu IP地址:

 

kevinqq@CN-00009841:/c/Users/xxx$ ifconfig eth1
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.21.96.1  netmask 255.255.255.240  broadcast 172.21.96.15

打开Win10浏览器,输入172.21.96.1:5000就看到Docker里的服务正常运行了:



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值