Linux我用 Linux我也码

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

2019-01-13  本文已影响13人  非梦nj

关键字:Win10 Ubuntu18 WSL Docker “remote Docker daemon”

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

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

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

下一篇:用docker启动一个Sanic微服务框架(TBD)https://github.com/kevinqqnj/sanic-ms

上一篇下一篇

猜你喜欢

热点阅读