Windows WSL雖然支持通過命令行直接運(yùn)行Linux系統(tǒng)發(fā)行版钧栖,但WSL并不支持Systemd,導(dǎo)致Linux系統(tǒng)的Docker無法以后臺服務(wù)的形式啟動实柠。通常的解決辦法為安裝Docker Desktop水泉,再設(shè)置WSL為后端來在Linux下運(yùn)行Docker,這種方式還是不太方便窒盐。
Distrod提供一種更簡單的方案草则,通過安裝Distrod或者使用預(yù)置Distrod的Linux發(fā)行版,解決在WSL下運(yùn)行Docker的問題蟹漓。
這里我選擇在Ubuntu 18.04上安裝Distrod的方式來運(yùn)行Docker炕横,畢竟我已經(jīng)安裝了Ubuntu的發(fā)行版。
安裝Distrod
- 通過Windows Terminal打開Ubuntu葡粒,并安裝Distrod份殿,執(zhí)行以下命令:
curl -L -O "https://raw.githubusercontent.com/nullpo-head/wsl-distrod/main/install.sh"
chmod +x install.sh
sudo ./install.sh install
- 在Ubuntu上使能
Distrod
,執(zhí)行/opt/distrod/bin/distrod enable
或者可以一勞永逸選擇在Windows啟動/opt/distrod/bin/distrod enable --start-on-windows-boot
塔鳍。
開打新的Windows終端窗口伯铣,如Power Shell或者CMD,執(zhí)行
wsl --terminate Ubuntu-18.04
(通過wsl --list
可以獲取所有的WSL發(fā)行版)轮纫。打開新的Ubuntu 窗口腔寡,執(zhí)行
sudo service docker start
或者sudo systemctl enable docker.service && sudo systemctl start docker.service
啟動Docker服務(wù)。
運(yùn)行Docker
簡單運(yùn)行一個nginx的鏡像:
iambowen@LAPTOP-ESJAVHR4:~$ docker run -d -p 8080:80 nginx:alpine
Unable to find image 'nginx:alpine' locally
alpine: Pulling from library/nginx
df9b9388f04a: Pull complete
5867cba5fcbd: Pull complete
4b639e65cb3b: Pull complete
061ed9e2b976: Pull complete
bc19f3e8eeb1: Pull complete
4071be97c256: Pull complete
Digest: sha256:5a0df7fb7c8c03e4158ae9974bfbd6a15da2bdfdeded4fb694367ec812325d31
Status: Downloaded newer image for nginx:alpine
4dbd96be91aa8e836027fb94318992a9ee2e88945e7b5e0241c05ab8007517e3
iambowen@LAPTOP-ESJAVHR4:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4dbd96be91aa nginx:alpine "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp friendly_pasteur
在Windows的瀏覽器中輸入127.0.0.1:8080
就可以看到Nginx歡迎界面了掌唾。