go-sqlcmd
go-sqlcmd copied to clipboard
Fix silent wget failures in DownloadFile causing corrupted .bak file downloads
When downloading .bak files from HTTP servers during container creation, users experienced mysterious "corrupted file" errors despite files appearing to download successfully. The issue was caused by silent wget failures that weren't being detected or reported.
Problem
Users running commands like:
sqlcmd create mssql --name express --accept-eula --using http://localhost:3000/files/WideWorldImporters-Full.bak
Would see:
- Files appeared to download (correct size shown in container)
- But database restore failed with "The volume on device '/var/opt/mssql/backup/WideWorldImporters-Full.bak' is empty"
- No error messages during the download process
Root Cause
The DownloadFile function in internal/container/controller.go was:
- Ignoring wget errors: The function called
runCmdInContainer()but ignored the returned stdout/stderr - Using incompatible wget flags: GNU wget flags didn't work with BusyBox wget in Alpine containers
- Not validating downloads: No verification that files actually existed after download
When containers tried to access localhost URLs (which fails due to container networking), wget would fail silently.
Solution
1. Added Comprehensive Error Detection
// Before: Silent failures
c.runCmdInContainer(id, cmd)
// After: Proper error checking
stdout, stderr = c.runCmdInContainer(id, cmd)
if len(stderr) > 0 {
stderrStr := string(stderr)
if strings.Contains(stderrStr, "Connection refused") ||
strings.Contains(stderrStr, "404 Not Found") ||
// ... other error conditions {
panic("wget download failed: " + stderrStr)
}
}
2. Fixed BusyBox Compatibility
// Before: GNU wget flags (unsupported)
cmd = []string{"wget", "--no-check-certificate", "--timeout=300", "--tries=3", "--progress=dot:mega", "-O", file, src}
// After: BusyBox-compatible flags
cmd = []string{"wget", "-T", "300", "-t", "3", "-O", file, src}
3. Added File Validation
- Verify downloaded files actually exist
- Provide clear error messages when downloads fail
User Impact
Before Fix
Downloading WideWorldImporters-Full.bak
Restoring database WideWorldImporters-Full
ERROR: The volume on device '/var/opt/mssql/backup/WideWorldImporters-Full.bak' is empty.
After Fix
Downloading WideWorldImporters-Full.bak
panic: wget download failed: Connecting to 127.0.0.1:3000 (127.0.0.1:3000)
wget: can't connect to remote host (127.0.0.1): Connection refused
Users now get clear, actionable error messages and can use proper solutions:
- External URLs:
https://example.com/files/database.bak - Host networking:
http://host.docker.internal:3000/files/database.bak - Proper container networking configuration
Testing
- Added comprehensive error detection tests
- Verified existing functionality remains intact
- Added documentation about container networking limitations
Fixes #566.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.