Skip to content

Instantly share code, notes, and snippets.

@gertd
Last active January 10, 2020 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gertd/1b02143fe7fb1e086523f239e059f94f to your computer and use it in GitHub Desktop.
Save gertd/1b02143fe7fb1e086523f239e059f94f to your computer and use it in GitHub Desktop.
MS SQL Server container helper script
#!/usr/bin/env bash
name="sql2017"
volume=sqlvolume
function initService {
if [ ! "$(docker ps -q -f name=$name)" ]; then
echo -n "SQL Server SA password: "
read -s password
echo -n
docker run \
-e "ACCEPT_EULA=Y" \
-e "SA_PASSWORD=$password" \
-e "MSSQL_PID=Developer" \
-p 1433:1433 \
--name $name \
-v $volume:/var/opt/mssql \
-d mcr.microsoft.com/mssql/server:2017-latest
else
echo container $name is already running
fi
}
function startService {
if [ "$(docker ps -q -f name=$name)" ]; then
echo container $name is already running
elif [ ! "$(docker ps -a -q -f name=$name)" ]; then
echo container $name not present use mssql-ctl init to create
else
docker start $name
fi
}
function stopService {
if [ "$(docker ps -q -f name=$name)" ]; then
docker stop $name
fi
}
function restartService {
stopService
startService
}
function deleteService {
stopService
if [ "$(docker ps -a -f name=$name)" ]; then
docker rm $name
fi
if [ "$(docker volume ls -q -f name=$volume)" ]; then
docker volume rm $volume
fi
}
function invokeSqlCmd {
if [ "$(docker ps -q -f name=$name)" ]; then
docker exec -ti $name /opt/mssql-tools/bin/sqlcmd -Usa -Slocalhost
else
echo container $name is not running
fi
}
function invokeShell {
if [ "$(docker ps -q -f name=$name)" ]; then
docker exec -ti $name /bin/bash
else
echo container $name is not running
fi
}
function displayUsage {
echo "usage: mssql-ctl [start|stop|restart|sqlcmd|bash|init|delete]"
}
case "$1" in
init)
initService
;;
start)
startService
;;
stop)
stopService
;;
restart)
restartService
;;
delete)
deleteService
;;
sqlcmd)
invokeSqlCmd
;;
bash)
invokeShell
;;
*)
displayUsage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment