Skip to content

Instantly share code, notes, and snippets.

@michaeltcoelho
Last active December 18, 2018 23:01
Show Gist options
  • Save michaeltcoelho/188611df91791cea310272eda6cf6496 to your computer and use it in GitHub Desktop.
Save michaeltcoelho/188611df91791cea310272eda6cf6496 to your computer and use it in GitHub Desktop.
Running MSSQLServer on Docker
Running the SQL SERVER container
docker run -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=teste123$ --name medizin -p 1400:1433 -v data:/var/opt/mssql -dd mcr.microsoft.com/mssql/server:2017-latest
Change SA user password
docker exec -it medizin /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U SA -P ‘teste123$’ -Q 'ALTER LOGIN SA WITH PASSWORD=“teste123$”’
Creating backup folder inside the container
docker exec -it medizin mkdir /var/opt/mssql/backup
Send .bak file to the backup folder inside the container
docker cp backup/YOUR_BACKUP_FILE.bak medizin:/var/opt/mssql/backup
Filelist the .bak file
docker exec -it medizin /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P teste123$ -Q 'RESTORE FILELISTONLY FROM DISK = "/var/opt/mssql/backup/Medizin_8942201809190312.bak" ' | tr -s ' ' | cut -d ' ' -f 1-2
Restoring database
docker exec -it medizin /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P teste123$ -Q 'RESTORE DATABASE Medizin FROM DISK = "/var/opt/mssql/backup/Medizin_8942201809190312.bak" WITH MOVE "medizin_8942_Data" TO "/var/opt/mssql/data/Medizin_userdata.ndf", MOVE "medizin_8942_Log" TO "/var/opt/mssql/data/Medizin.ldf"'
Select Databases
docker exec -it medizin /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P teste123$ -Q 'SELECT Name FROM sys.Databases'
Running SQL Queries
docker exec -it medizin /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P teste123$ -Q “<YOUR QUERY HERE>”
Get All tables
docker exec -it medizin /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P teste123$ -Q "SELECT * FROM Medizin.INFORMATION_SCHEMA.Tables"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment