# Export Docker image from local and upload to server param( [string]$Server = "45.249.208.118", [string]$User = "root", [string]$Password = "lxvhNYFZ4160", [string]$ImageName = "node:18-alpine" ) $ErrorActionPreference = "Stop" Write-Host "Exporting and uploading Docker image..." -ForegroundColor Cyan $plinkPath = "C:\Program Files\PuTTY\plink.exe" $pscpPath = "C:\Program Files\PuTTY\pscp.exe" if (-not (Test-Path $plinkPath)) { Write-Host "Error: plink.exe not found" -ForegroundColor Red exit 1 } if (-not (Test-Path $pscpPath)) { Write-Host "Error: pscp.exe not found" -ForegroundColor Red exit 1 } # Check if Docker is available locally Write-Host "`n[1/6] Checking local Docker..." -ForegroundColor Yellow try { $dockerVersion = docker --version 2>&1 Write-Host $dockerVersion -ForegroundColor Green } catch { Write-Host "Error: Docker not found locally" -ForegroundColor Red Write-Host "Please install Docker Desktop or Docker CLI" -ForegroundColor Yellow exit 1 } # Check if image exists locally Write-Host "`n[2/6] Checking if image exists locally..." -ForegroundColor Yellow $imageExists = docker images $ImageName --format "{{.Repository}}:{{.Tag}}" 2>&1 if ($imageExists -match $ImageName) { Write-Host "Image found: $ImageName" -ForegroundColor Green } else { Write-Host "Image not found locally. Pulling from Docker Hub..." -ForegroundColor Yellow Write-Host "This may take a few minutes..." -ForegroundColor Cyan docker pull $ImageName if ($LASTEXITCODE -ne 0) { Write-Host "Error: Failed to pull image from Docker Hub" -ForegroundColor Red Write-Host "Please check your local internet connection" -ForegroundColor Yellow exit 1 } Write-Host "Image pulled successfully" -ForegroundColor Green } # Export image Write-Host "`n[3/6] Exporting image to tar file..." -ForegroundColor Yellow $imageTar = Join-Path $env:TEMP "node-18-alpine.tar" $imageTarName = "node-18-alpine.tar" if (Test-Path $imageTar) { Remove-Item $imageTar -Force } docker save $ImageName -o $imageTar if ($LASTEXITCODE -ne 0) { Write-Host "Error: Failed to export image" -ForegroundColor Red exit 1 } $fileSize = (Get-Item $imageTar).Length / 1MB Write-Host "Image exported: $([math]::Round($fileSize, 2)) MB" -ForegroundColor Green # Compress image (optional but recommended for large files) Write-Host "`n[4/6] Compressing image..." -ForegroundColor Yellow $compressedTar = "$imageTar.gz" if (Test-Path $compressedTar) { Remove-Item $compressedTar -Force } # Use gzip if available, otherwise upload uncompressed $hasGzip = $false try { $gzipVersion = gzip --version 2>&1 if ($LASTEXITCODE -eq 0) { $hasGzip = $true } } catch { $hasGzip = $false } if ($hasGzip) { gzip -c $imageTar > $compressedTar $compressedSize = (Get-Item $compressedTar).Length / 1MB Write-Host "Image compressed: $([math]::Round($compressedSize, 2)) MB" -ForegroundColor Green $uploadFile = $compressedTar $remoteFile = "node-18-alpine.tar.gz" } else { Write-Host "gzip not found, uploading uncompressed file" -ForegroundColor Yellow $uploadFile = $imageTar $remoteFile = $imageTarName } # Upload to server Write-Host "`n[5/6] Uploading image to server..." -ForegroundColor Yellow Write-Host "This may take a while depending on file size and network speed..." -ForegroundColor Cyan & $pscpPath -pw $Password -batch $uploadFile "${User}@${Server}:/tmp/$remoteFile" if ($LASTEXITCODE -ne 0) { Write-Host "Error: Failed to upload image" -ForegroundColor Red exit 1 } Write-Host "Image uploaded successfully" -ForegroundColor Green # Load image on server Write-Host "`n[6/6] Loading image on server..." -ForegroundColor Yellow if ($hasGzip) { # Decompress and load $loadCommand = "gunzip -c /tmp/$remoteFile | docker load" } else { # Load directly $loadCommand = "docker load -i /tmp/$remoteFile" } $loadResult = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "$loadCommand 2>&1" Write-Host $loadResult # Cleanup remote file Write-Host "`nCleaning up remote file..." -ForegroundColor Yellow & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "rm -f /tmp/$remoteFile" # Cleanup local files Write-Host "Cleaning up local files..." -ForegroundColor Yellow Remove-Item $imageTar -Force -ErrorAction SilentlyContinue if ($hasGzip) { Remove-Item $compressedTar -Force -ErrorAction SilentlyContinue } # Verify image on server Write-Host "`nVerifying image on server..." -ForegroundColor Yellow $verifyResult = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "docker images $ImageName 2>&1" Write-Host $verifyResult if ($verifyResult -match $ImageName) { Write-Host "`n[SUCCESS] Image successfully uploaded and loaded on server!" -ForegroundColor Green Write-Host "You can now deploy your application:" -ForegroundColor Green Write-Host " .\scripts\deploy\deploy-simple.ps1" -ForegroundColor Cyan } else { Write-Host "`n[WARN] Image may not have been loaded correctly" -ForegroundColor Yellow Write-Host "Please check the output above" -ForegroundColor Yellow }