關(guān)鍵字:Win10 Ubuntu18 WSL Docker “remote Docker daemon”
- 問(wèn)題:
- WSL如何完美配合Docker使用呢?難道一定要通過(guò)VirtualBox么,?或者一定要安裝單獨(dú)的Ubuntu系統(tǒng)么,?
- 答案:很簡(jiǎn)單,通過(guò)remote Docker daemon連接,!
- 優(yōu)點(diǎn):
- Docker基本無(wú)性能損失
- 無(wú)須另外安裝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
2. 在Win10 WSL里安裝Ubuntu
3. Ubuntu18安裝Docker CE
# Allow your user to access the Docker CLI without needing root access.
sudo usermod -aG docker $USER
# 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)鍵的一行命令,,打開(kāi)Bash:
echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc
不通過(guò)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?
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開(kāi)發(fā)時(shí)要注意Win10共享文件夾的問(wèn)題
Docker in Window里,只允許訪問(wèn)c:\Users\<id>\ 目錄下的東西,,Win+R - Bash打開(kāi)時(shí),,默認(rèn)目錄也是這個(gè)文件夾
WSL Ubuntu18里,默認(rèn)映射Win C盤(pán)為/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./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里命令行訪問(wèn),已經(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里訪問(wèn)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
打開(kāi)Win10瀏覽器,,輸入172.21.96.1:5000 就看到Docker里的服務(wù)正常運(yùn)行了:
下一篇:用docker啟動(dòng)一個(gè)Sanic微服務(wù)框架(TBD)https://github.com/kevinqqnj/sanic-ms
|