Skip to content

Instantly share code, notes, and snippets.

@SQLvariant
Created March 12, 2019 23:08
Show Gist options
  • Save SQLvariant/d3cefd84b1955a78e7c995b192998885 to your computer and use it in GitHub Desktop.
Save SQLvariant/d3cefd84b1955a78e7c995b192998885 to your computer and use it in GitHub Desktop.
Create a SQL-on-Linux container, and restore the AdventureWorks2016 DB
$BakURL = "https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks2016.bak"
$BakFile = "$($Home)\Downloads\AdventureWorks2016.bak"
Invoke-WebRequest -Uri $BakURL -OutFile $BakFile
mkdir C:\SQLData\Docker\SQLDev02
#dir C:\SQLData\Docker\SQLDev02 | OGV -PassThru | Remove-Item
Copy-Item -Path "$($Home)\Downloads\AdventureWorks2016.bak" -Destination C:\SQLData\Docker\SQLDev02
<# Create a Container with a volume mounted from the host #>
docker run -d -p 10002:1433 -v C:\SQLData\Docker\SQLDev02:/sqlserver --env ACCEPT_EULA=Y --env SA_PASSWORD=Test1234 --name testcontainer02 microsoft/mssql-server-linux:latest
<# Make sure to say Yes and add your username & password so Docker can connect to the volume-location. #>
<# via Invoke-SqlCmd to Host-Mount #>
New-StoredCredential -Target "SqlDocker" -UserName "sa" -Password "Test1234" -Persist LocalMachine
$cred = Get-StoredCredential -Target "SqlDocker"
Invoke-Sqlcmd -ServerInstance 'localhost,10002' -Credential $cred -Query "SELECT name from sys.databases"
#Requires -Modules SqlServer
Invoke-Sqlcmd -ServerInstance 'localhost,10002' -Credential $cred -Query "
RESTORE DATABASE [AdventureWorks2016] FROM DISK = N'/sqlserver/AdventureWorks2016.bak' WITH FILE = 1,
MOVE N'AdventureWorks2016_Data' TO N'/sqlserver/AdventureWorks2016_Data.mdf',
MOVE N'AdventureWorks2016_Log' TO N'/sqlserver/AdventureWorks2016_Log.ldf',
NOUNLOAD, STATS = 5"
Invoke-Sqlcmd -ServerInstance 'localhost,10002' -Credential $cred -Query "SELECT name from sys.databases"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment