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

tech2025-06-21  1

Windows10 内置了Linux系统:WSL (Windows Subsystem for Linux, 又称Bash for Windows)。可以方便地在Win10里使用Ubuntu等Linux系统的命令行Win10可以直接安装Docker in Windows,不需要像Win7一样,要配合VirtualBox。Ref: https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly

关键字: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

官网:https://docs.docker.com/docker-for-windows/install/

打开Docker Desktop设置: 确保勾选:Expose daemon on localhost:2375 without TLS

 

image.png

2. 在Win10 WSL里安装Ubuntu

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

 

image.png

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

3. Ubuntu18安装Docker CE

官网:https://docs.docker.com/install/linux/docker-ce/ubuntu/授于当前用户以root权限运行Docker CLI

 

# 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)