1. 程式人生 > >wsl中使用原生docker

wsl中使用原生docker

之前介紹過windows中安裝docker,但是它需要用到hyper-v。hyper-v與vm不相容非常之不方便。不過發現windows有wsl(linux子系統)遂試驗,結果非常nice功能一應俱全docker完美相容,與大家分享一下安裝過程。

開啟win10的linux子系統功能

  • 推薦win10升級到最新版本

開啟powershell執行命令 Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux 或者在控制面板中的程式中啟用適用於linux的winodws子系統(直譯?),然後重啟電腦 這時可以去microsoft store中選擇linux發行版,我用的是ubuntu18.04

建議大家與我一致。安裝完後開啟還需要下載一些檔案,大家只需要坐與放寬即可

cmd更換配色

在下載檔案時也別閒著,windowns自帶的cmd配色難看至極,一會使用vim的時候保你字都看不清。

  • 使用管理員開啟cmd進入解壓後的資料夾
  • 執行命令 colortool.exe d OneHalfLight(還有其他配色方案,在schemes下)
  • 視窗標題欄右鍵預設值儲存,屬性儲存

切換root賬戶

一頓操作下來ubuntu也該o98k了,開始設定使用者密碼,然後我們需要使用root賬戶

  • 管理員模式開啟ubuntu18.04
  • su passwd 輸入密碼與root密碼
  • su root 輸入密碼

但是這時每次開啟ubuntu的時候還是普通使用者,我們可以使用命令設定預設使用者 ubuntu1804 config --default-user root

安裝docker

linux子系統已經收工,下面安裝docker。我們可以直接根據docker文件中提供的ubuntu安裝docker的方法進行操作 ps:使用管理員開啟ubuntu1804

因為都知道的網路原因安裝時可能會timeout等其他情況,我們可以使用國內映象 https://mirrors.tuna.tsinghua.edu.cn/ 清華大學開源軟體映象站替換下面的連結 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/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 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

一頓操作已經安裝結束,使用 sudo service docker start 開啟docker守護程序 使用 docker version 檢視版本

The end

這時開發時就很方便了,也不用擔心hyper-v與vm不相容的問題。例如在vs code中我們可以很容器的在終端中進行子系統進行各種操作,例如在vs code使用終端進入linux子系統執行各種操作(linux與windows檔案共享)