--------------------2020/05/26更新-------------------------------
沒事折騰可以,不過如果需要生產實踐,還是老老實實,用虛擬機模擬生產的linux環境,wsl始終是不夠的。
--------------------原文------------------
參考:https://www.cnblogs.com/sweetsunnyflower/p/10543318.html
安裝wsl
首先在 程序和功能
->啓用和關閉windows功能
中打開適用於Linux的Windows子系統
然後打開微軟應用商店,直接搜索Ubuntu,選擇18.06版本的ubuntu安裝
wsl配置
首先配置阿里雲鏡像,vim /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
然後更新源
apt update
默認情況下,windows的目錄會自動掛載(mount)到wsl中的/mnt目錄下,但是這樣會導致後面的docker的相對路徑問題。所以修改配置文件 /etc/wsl.conf
[automount]
root = /
options = "metadata,umask=22,fmask=11"
這樣,windows裏面的c盤就自動掛載到了wsl中的/c/目錄下,d盤就自動掛載到了wsl中的/d/目錄下
安裝docker for windows
直接到官網上下載安裝即可。
安裝的時候,因爲我們要使用wsl中的docker,也就是linux container,所以記得不要選擇windows container。
安裝好了之後,先配置加速器,目前我用阿里雲和daocloud的加速器,都挺快的。到對應的網站上按照指示操作即可。
開發的時候,需要把物理機上的代碼和容器中的代碼文件做共享,所以需要在Shared Drives
中配置共享的盤符。你的代碼在哪個盤裏面,那麼就選擇共享哪個盤
wsl中安裝docker
如果直接用apt來安裝docker,不會是最新版的,所以參考官方文檔來安裝最新版的docker(https://docs.docker.com/install/linux/docker-ce/ubuntu/)
即:
sudo apt-get remove docker docker-engine docker.io
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
試一下執行docker命令:
docker version
結果如下:
Client:
Version: 18.03.1-ce
API version: 1.37
Go version: go1.9.5
Git commit: 9ee9f40
Built: Wed Jun 20 21:43:51 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.03.1-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: 9ee9f40
Built: Thu Apr 26 07:22:38 2018
OS/Arch: linux/amd64
Experimental: false
OK了,但是如果再run一下呢?
docker run busybox
此時會提示docker daemon沒有運行。那麼執行:
sudo service docker start
雖然看到is starting,但是docker還是不能run。
此時,就需要打開docker for windows中的General->Expose daemon on tcp://localhost:2375 without TLS
然後在wsl中執行:
export DOCKER_HOST=tcp://localhost:2375
然後就可以開始run了。
另外默認情況下是不會安裝docker-compose的,如果通過apt來安裝docker-compose,也不會是最新版,但是通過pip來安裝的就是最新版,所以:
sudo apt install python-pip
sudo pip install docker-compose
測試下:
docker-compose version