### Linux 環境安裝 開發人員可以在一台 Windows 機器上同時使用 Windows 和 Linux 的功能。 Windows Subsystem for Linux(WSL)讓開發人員能夠安裝一個 Linux 發行版(如 Ubuntu、OpenSUSE、Kali、Debian、Arch Linux 等) 1. WSL 安裝準備 系統需求:確保你的 Windows 系統版本是 Windows 10 2004(建立19041)或更高版本。 安裝 WSL: 用管理員身份運行 PowerShell ,輸入以下指令以安裝WSL。 `wsl --install` 這個指令將會自動開啟必要的 Windows 功能(包括虛擬機平台和 WSL),並且下載最新的 Linux 核心。 2. 重啟電腦 3. 輸入以下指令來安裝 Ubuntu `wsl --install -d Ubuntu` 4. 檢查是否安裝完成 `wsl --list` 5. 啟動 Ubuntu `wsl -d Ubuntu` 移除 Ubuntu (補充) `wsl --unregister Ubuntu` ### 安裝 Docker 1. 在安裝 Docker Engine 之前,需要解除安裝所有衝突的軟體包 `for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done` 2. 設定 Docker 的 APT 儲存庫 ```= # Add Docker's official GPG key: # Step 1 # 這個命令會更新系統的套件清單,確保你安裝的軟體是最新的版本。 sudo apt-get update # Step 2 # 這個命令會安裝 ca-certificates 和 curl 軟體包。 # ca-certificates 用於管理 HTTPS 證書 # curl 則是一個用於發送 HTTP 請求的工具,通常用於下載文件或內容。 sudo apt-get install ca-certificates curl # Step 3 # 這個命令會創建一個目錄 /etc/apt/keyrings,用於存放軟體源的 GPG 金鑰。 sudo install -m 0755 -d /etc/apt/keyrings # Step 4 # 這個命令使用 curl 從 Docker 官方網站下載一個 GPG 金鑰文件 docker.asc,並保存到剛剛創建的 /etc/apt/keyrings 目錄中。 sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc # Step 5 # 這個命令設置 docker.asc 文件的權限,讓所有用戶都可以讀取該文件。這是為了確保系統可以使用這個金鑰來驗證 Docker 軟體包的真實性。 sudo chmod a+r /etc/apt/keyrings/docker.asc # Step 6 # 這段命令的作用是將 Docker 的 APT 儲存庫資訊添加到你的系統中的 /etc/apt/sources.list.d/ 目錄下,以便系統在更新軟體清單時能夠包含 Docker 的相關資訊。 # 具體來說: # 這個部分會輸出一段文字,其中包含了 Docker 的 APT 儲存庫的 URL、系統的架構、以及簽名金鑰的位置等等。 echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update ``` 3. 安裝 Docker 最新版 `sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin` 指定版本 `VERSION_STRING=5:24.0.0-1~ubuntu.22.04~jammy` `sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin` 4. 測試是否安裝成功 `docker images` ### Docker 問題處理 1. Docker 啟不起來 出現`docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?` 並且輸入`docker info`顯示 ``` Server: ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? errors pretty printing info ``` 後來參考這篇文章解決 https://www.cnblogs.com/zhouXX/p/17007216.html 原因是由於新的 Ubuntu 系統使用了 iptables-nft ,而 WSL2 不支援導致的。 ``` mcken@MSI:/mnt/c/WINDOWS/system32$ vim /var/log/docker.log mcken@MSI:/mnt/c/WINDOWS/system32$ sudo update-alternatives --config iptables Press <enter> to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/sbin/iptables-legacy to provide /usr/sbin/iptables (iptables) in manual mode ``` 2. 添加群組 如果不想在 Docker 命令前面加上 sudo 2.1 建立 Docker 群組 `sudo groupadd docker` 2.2 將您的用戶新增至 Docker 群組。 `sudo usermod -aG docker $USER` 2.3 重新啟動 2.4 測試 `docker run hello-world` 參考資料 https://bayareanotes.com/wsl-installation/ https://docs.docker.com/engine/install/linux-postinstall/
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up