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

18 篇文章 ¥29.90 ¥99.00
本文介绍了如何在Windows10的Windows Subsystem for Linux (WSL)环境下,通过安装Ubuntu并配置Docker,实现与Docker in Windows的无缝配合。无需VirtualBox,只需通过remote Docker daemon连接,即可实现Docker的高效使用。详细步骤包括安装Docker Desktop,设置WSL的Ubuntu,安装Docker CE,以及解决Win10共享文件夹问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键字: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里的服务正常运行了:



 

### Win11家庭版Docker Desktop安失败解决方案 对于Win11家庭版用户,在尝试安Docker Desktop时可能会遭遇多种错误,如启动时报错“An unexpected error occurred. Restart Docker Desktop”[^1] 或者持续显示 “Docker Engine Stopped”[^2]。这些问题的根本原因之一在于Windows 11家庭版默认不支持Hyper-V特性,而Docker Desktop依赖于该虚拟化技术来运行容器环境。 #### Hyper-V未启用或不可用 当在Windows 11家庭中文版中遇到Hyper-V启用失败的情况时,这是由于家庭版本的操作系统并不自带Hyper-V组件所致[^3]。为了克服这一障碍并成功部署Docker Desktop,可以考虑以下几种方法: - **升级至更高版本的Windows**:专业版及以上级别的Windows操作系统内置了完整的Hyper-V功能集,可以直接满足Docker的要求。 - **利用WSL 2作为替代方案** 如果不想更换操作系统版本,则可以通过配置Windows Subsystem for Linux (WSL) 版本2来绕过这个问题。具体操作如下: - 启用适用于LinuxWindows子系统以及虚拟机平台服务: ```powershell wsl --install dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart ``` - 设置默认使用的WSL版本为2: ```powershell wsl --set-default-version 2 ``` - 下载并设置所需的Linux发行版(例如Ubuntu),通过Microsoft Store获取非常便捷。 - 配置好之后再次尝试安Docker Desktop,并确保选择了基于WSL 2模式的工作流选项。 上述措施能够有效帮助解决因缺少必要的底层技术支持而导致的Docker Desktop安难题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值