- Windows10 內(nèi)置了Linux系統(tǒng):WSL (Windows Subsystem for Linux, 又稱Bash for Windows)兵睛〉湔螅可以方便地在Win10里使用Ubuntu等Linux系統(tǒng)的命令行
- Win10可以直接安裝Docker in Windows苦丁,不需要像Win7一樣浸颓,要配合VirtualBox。
- Ref: https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly
關(guān)鍵字:Win10 Ubuntu18 WSL Docker “remote Docker daemon”
- 問題:
- WSL如何完美配合Docker使用呢旺拉?難道一定要通過VirtualBox么产上?或者一定要安裝單獨(dú)的Ubuntu系統(tǒng)么?
- 答案:很簡單蛾狗,通過remote Docker daemon連接晋涣!
- 優(yōu)點(diǎn):
- Docker基本無性能損失
- 無須另外安裝VirtualBox等虛擬機(jī)
- 方便同時(shí)使用Win10、Ubuntu兩個(gè)系統(tǒng)
框架
Win10 + Docker <-- 本機(jī)共享文件夾Volume --> Ubuntu命令行(in WSL)
Ubuntu --> remote Docker daemon --> Docker in Windows
Win10瀏覽器 --> Docker in Windows --> Ubuntu IP --> Ubuntu內(nèi)服務(wù)
步驟
1. 安裝Docker in Windows10
- 官網(wǎng):https://docs.docker.com/docker-for-windows/install/
-
打開Docker Desktop設(shè)置:
確保勾選:Expose daemon on localhost:2375 without TLS
image.png
2. 在Win10 WSL里安裝Ubuntu
-
這教程太多了沉桌,自行搜索谢鹊。大致是打開虛擬化HyperV,安裝Windows Feature: WSL留凭,重啟佃扼,微軟商店里安裝“Ubuntu18.04”
image.png - 安裝完畢,通過開始菜單 - Ubuntu打開蔼夜,設(shè)置Linux用戶名/密碼
- Win+R - “Bash”也能打開兼耀,注意跟上一步起始目錄的區(qū)別
3. Ubuntu18安裝Docker CE
- 官網(wǎng):https://docs.docker.com/install/linux/docker-ce/ubuntu/
- 授于當(dāng)前用戶以root權(quán)限運(yùn)行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
- 最關(guān)鍵的一行命令,打開Bash:
echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc
不通過deamon連接的話求冷,你在Ubuntu里運(yùn)行docker瘤运,就會(huì)出現(xiàn)錯(cuò)誤:
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
- 驗(yàn)證Docker啟動(dòng)成功:
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.
至此,已經(jīng)在WSL Ubuntu里完美配置Docker成功了匠题!
5. 進(jìn)階:使用Docker開發(fā)時(shí)要注意Win10共享文件夾的問題
Docker in Window里拯坟,只允許訪問
c:\Users\<id>\
目錄下的東西,Win+R - Bash打開時(shí)梧躺,默認(rèn)目錄也是這個(gè)文件夾
WSL Ubuntu18里似谁,默認(rèn)映射Win C盤為/mnt/c/
,需要手動(dòng)改為/c/
# Running Windows 10 17.09?
sudo mkdir /c
sudo mount --bind /mnt/c /c
測(cè)試一下docker-compose一個(gè)Flask網(wǎng)站:
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)
此時(shí)掠哥,在Ubuntun里命令行訪問巩踏,已經(jīng)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里的服務(wù)正常運(yùn)行了:
下一篇:用docker啟動(dòng)一個(gè)Sanic微服務(wù)框架(TBD)https://github.com/kevinqqnj/sanic-ms