Win10內(nèi)置Ubuntu,完美使用Docker in Windows

關(guān)鍵字:Win10 Ubuntu18 WSL Docker “remote Docker daemon”

  • 問題:
    • WSL如何完美配合Docker使用呢?難道一定要通過VirtualBox么?或者一定要安裝單獨的Ubuntu系統(tǒng)么?
    • 答案:很簡單,通過remote Docker daemon連接!
  • 優(yōu)點:
    • Docker基本無性能損失
    • 無須另外安裝VirtualBox等虛擬機
    • 方便同時使用Win10、Ubuntu兩個系統(tǒng)
image.png

框架

Win10 + Docker <-- 本機共享文件夾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

2. 在Win10 WSL里安裝Ubuntu

  • 這教程太多了,自行搜索。大致是打開虛擬化HyperV,安裝Windows Feature: WSL,重啟,微軟商店里安裝“Ubuntu18.04”


    image.png
  • 安裝完畢,通過開始菜單 - Ubuntu打開,設(shè)置Linux用戶名/密碼
  • Win+R - “Bash”也能打開,注意跟上一步起始目錄的區(qū)別

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

  • 最關(guān)鍵的一行命令,打開Bash:
echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc

不通過deamon連接的話,你在Ubuntu里運行docker,就會出現(xiàn)錯誤:

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.

至此,已經(jīng)在WSL Ubuntu里完美配置Docker成功了!

5. 進階:使用Docker開發(fā)時要注意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網(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)

此時,在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ù)正常運行了:

下一篇:用docker啟動一個Sanic微服務(wù)框架(TBD)https://github.com/kevinqqnj/sanic-ms

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容