Last active
March 24, 2021 19:45
-
-
Save danielplawgo/dcc35f9b7b5343d0fb3378550df24601 to your computer and use it in GitHub Desktop.
SQL Server i Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'Database1') | |
BEGIN | |
CREATE DATABASE [Database1] | |
END | |
GO | |
USE [Database1] | |
GO | |
--You need to check if the table exists | |
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Users' and xtype='U') | |
BEGIN | |
CREATE TABLE Users ( | |
Id INT PRIMARY KEY IDENTITY (1, 1), | |
Name VARCHAR(100) | |
) | |
END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.9" | |
services: | |
db: | |
container_name: sql-server-db | |
image: mcr.microsoft.com/mssql/server:2019-latest | |
environment: | |
SA_PASSWORD: Your_password123 | |
ACCEPT_EULA: Y | |
ports: | |
- 1433:1433 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.9" | |
services: | |
db: | |
container_name: sql-server-db | |
image: mcr.microsoft.com/mssql/server:2019-latest | |
environment: | |
SA_PASSWORD: Your_password123 | |
ACCEPT_EULA: Y | |
ports: | |
- 1433:1433 | |
volumes: | |
- ./sqlserver/data:/var/opt/mssql/data | |
- ./sqlserver/log:/var/opt/mssql/log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.9" | |
services: | |
db: | |
container_name: sql-server-db | |
image: mcr.microsoft.com/mssql/server:2019-latest | |
environment: | |
SA_PASSWORD: Your_password123 | |
ACCEPT_EULA: Y | |
ports: | |
- 1433:1433 | |
volumes: | |
- ./sqlserver/data:/var/opt/mssql/data | |
- ./sqlserver/log:/var/opt/mssql/log | |
- ./scripts:/scripts/ | |
command: | |
- /bin/bash | |
- -c | |
- | | |
# Launch MSSQL and send to background | |
/opt/mssql/bin/sqlservr & | |
pid=$$! | |
# Wait for it to be available | |
echo "Waiting for MS SQL to be available ⏳" | |
/opt/mssql-tools/bin/sqlcmd -l 30 -S localhost -h-1 -V1 -U sa -P $$SA_PASSWORD -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , @@servername" | |
is_up=$$? | |
while [ $$is_up -ne 0 ] ; do | |
echo -e $$(date) | |
/opt/mssql-tools/bin/sqlcmd -l 30 -S localhost -h-1 -V1 -U sa -P $$SA_PASSWORD -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , @@servername" | |
is_up=$$? | |
sleep 5 | |
done | |
# Run every script in /scripts | |
# TODO set a flag so that this is only done once on creation, | |
# and not every time the container runs | |
for foo in /scripts/*.sql | |
do /opt/mssql-tools/bin/sqlcmd -U sa -P $$SA_PASSWORD -l 30 -e -i $$foo | |
done | |
# trap SIGTERM and send same to sqlservr process for clean shutdown | |
trap "kill -15 $$pid" SIGTERM | |
# Wait on the sqlserver process | |
echo "All scripts have been executed. Waiting for MS SQL(pid $$pid) to terminate." | |
# Wait on the sqlserver process | |
wait $$pid | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment