Docker的相關指令

Docker的相關指令

Table of Contents

空間清理

刪除所有未使用的資源(包括停止的容器、未使用的網路與懸空的映像)

docker system prune -a

清理未使用的磁碟卷(Volumes)注意:這會刪除所有未掛載的數據卷,請確認資料已備份)

docker volume prune

壓縮vhdx

//1. 確實關閉Docker Desktop

//2. Powershell
wsl --shutdown

//3. Powershell
diskpart
select vdisk file="C:\Users\XXX\AppData\Local\Docker\wsl\disk\filename.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit

以下提供批次的執行方式

1. 以Powershell執行以下指令

    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

    2. 製作ps1檔,將以下內容貼為xxx.ps1

    chcp 65001
    
    Write-Host "正在關閉 Docker Desktop..." -ForegroundColor Cyan
    Stop-Process -Name "Docker Desktop" -ErrorAction SilentlyContinue
    
    # 2. 徹底關閉 WSL
    Write-Host "正在關閉 WSL 子系統..." -ForegroundColor Cyan
    wsl --shutdown
    
    # 3. 定義 VHDX 路徑 (請確認此路徑與你的實際路徑一致)
    $vhdxPath = "C:\Users\UserName\AppData\Local\Docker\wsl\disk\DockerFile.vhdx"
    
    # 4. 建立 diskpart 暫存指令檔
    $tempFile = New-TemporaryFile
    @"
    select vdisk file="$vhdxPath"
    attach vdisk readonly
    compact vdisk
    detach vdisk
    exit
    "@ | Out-File -FilePath $tempFile -Encoding ascii
    
    # 5. 執行 diskpart 進行物理壓縮
    Write-Host "開始壓縮 VHDX 檔案,這可能需要幾分鐘..." -ForegroundColor Yellow
    diskpart /s $tempFile
    
    # 6. 清理暫存檔並啟動 Docker
    Remove-Item $tempFile
    Write-Host "壓縮完成!正在重新啟動 Docker Desktop..." -ForegroundColor Green
    Start-Process "C:\Program Files\Docker\Docker\Docker Desktop.exe"
    
    Read-Host "執行結束,請按 Enter 鍵關閉視窗..."

    3. 在該ps1檔點選滑鼠右鍵,選擇以Powershell執行即可。

    尋找容器

    列出所有容器名稱

    docker ps --format "{{.Names}}"

    找出容器ID

    docker inspect --format='{{.Id}}' wordpressWgt

    尋找wp-config.php在什麼地方

    find /volumeN/@docker -name "wp-config.php"後

    確認容器

    //在容器內產生標記檔案
    docker exec wordpressWgt touch /bitnami/wordpress/IDENTIFY_ME
    
    //尋找標記檔案
    find /volumeN/@docker -name "IDENTIFY_ME"

    找到正確的id

    cd $(docker inspect --format='{{.GraphDriver.Data.MergedDir}}' wordpressWgt)/opt/bitnami/wordpress