From 61a59a41ded661187ddd52a33c184077c9f19cc4 Mon Sep 17 00:00:00 2001 From: Rodrigo Ramos Date: Tue, 7 Sep 2021 17:09:43 -0300 Subject: [PATCH 1/2] added option to attach a database --- docker-entrypoint-initdb.sh | 45 +++++++++++++++++++++++++++---------- readme.md | 6 +++++ 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/docker-entrypoint-initdb.sh b/docker-entrypoint-initdb.sh index 17e225d..f47a032 100644 --- a/docker-entrypoint-initdb.sh +++ b/docker-entrypoint-initdb.sh @@ -5,18 +5,39 @@ if [ ! -f ~/init.lock ]; then # wait for database to start... - for i in {40..0}; do - if /opt/mssql-tools/bin/sqlcmd -U SA -P $SA_PASSWORD -Q 'SELECT 1;' &> /dev/null; then - echo "$0: SQL Server started" - break - fi - echo "$0: SQL Server startup in progress..." - sleep 1 - done - - echo "$0: Initializing database" - - touch ~/tmp.sql + for i in {60..0}; do + if /opt/mssql-tools/bin/sqlcmd -U SA -P $SA_PASSWORD -Q 'SELECT 1;' &> /dev/null; then + echo "$0: SQL Server started" + break + fi + echo "$0: SQL Server startup in progress..." + sleep 1 + done + + echo "$0: Initializing database" + + touch ~/tmp.sql + + if [ "$MSSQL_ATTACH_DATABASE_NAME" -a "$MSSQL_ATTACH_DATABASE_PATH" ]; then + + touch ~/attach_tmp.sql + cat > ~/attach_tmp.sql <<-EOATTACHSQL + USE [master] + GO + + CREATE DATABASE [${MSSQL_ATTACH_DATABASE_NAME}] ON + ( FILENAME = N'${MSSQL_ATTACH_DATABASE_PATH}/${MSSQL_ATTACH_DATABASE_NAME}.mdf' ), + ( FILENAME = N'${MSSQL_ATTACH_DATABASE_PATH}/${MSSQL_ATTACH_DATABASE_NAME}_log.ldf' ) + FOR ATTACH + + GO + +EOATTACHSQL + + /opt/mssql-tools/bin/sqlcmd -U SA -P $SA_PASSWORD -i ~/attach_tmp.sql + + rm -f ~/attach_tmp.sql + fi #BEGIN DATABASE CREATION if [ "$MSSQL_DATABASE" ]; then diff --git a/readme.md b/readme.md index f78901b..c126705 100644 --- a/readme.md +++ b/readme.md @@ -57,6 +57,12 @@ MSSQL_PID is the Product ID (PID) or Edition that the container will run with. A This image extends official mssql-server-linux. See more on [official **mssql-server-linux** image](https://hub.docker.com/r/microsoft/mssql-server-linux/) to understand license and find other Product Id's. +### **MSSQL_ATTACH_DATABASE_NAME** and **MSSQL_ATTACH_DATABASE_PATH** +These environment variables are optional, used to attach a database to server. +- **MSSQL_ATTACH_DATABASE_NAME** indicates the name of database **and** is used to determine the name of the ``.mdf`` and ``.ldf`` files; +- **MSSQL_ATTACH_DATABASE_PATH** indicates the base path of the ``.mdf`` and ``.ldf`` files; + +For example, using **MSSQL_ATTACH_DATABASE_NAME** = MyDatabase and **MSSQL_ATTACH_DATABASE_PATH** = /var/opt/mssql/data/custom-location/ will attach a database named **MyDatabase** thas uses **/var/opt/mssql/data/custom-location/MyDatabase**.mdf and **/var/opt/mssql/data/custom-location/MyDatabase**_log.ldf files; ## Volumes From 0d22a5919d72d3d07f769c9f92d2bd619599653b Mon Sep 17 00:00:00 2001 From: Rodrigo Ramos dos Santos Date: Tue, 7 Sep 2021 17:22:32 -0300 Subject: [PATCH 2/2] Adjusting readme --- readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index c126705..8c7da55 100644 --- a/readme.md +++ b/readme.md @@ -62,7 +62,8 @@ These environment variables are optional, used to attach a database to server. - **MSSQL_ATTACH_DATABASE_NAME** indicates the name of database **and** is used to determine the name of the ``.mdf`` and ``.ldf`` files; - **MSSQL_ATTACH_DATABASE_PATH** indicates the base path of the ``.mdf`` and ``.ldf`` files; -For example, using **MSSQL_ATTACH_DATABASE_NAME** = MyDatabase and **MSSQL_ATTACH_DATABASE_PATH** = /var/opt/mssql/data/custom-location/ will attach a database named **MyDatabase** thas uses **/var/opt/mssql/data/custom-location/MyDatabase**.mdf and **/var/opt/mssql/data/custom-location/MyDatabase**_log.ldf files; +#### Example +Using `MSSQL_ATTACH_DATABASE_NAME = MyDatabase` and `MSSQL_ATTACH_DATABASE_PATH = /var/opt/mssql/data/custom-location/` will attach a database named **MyDatabase** thas uses **/var/opt/mssql/data/custom-location/MyDatabase**.mdf and **/var/opt/mssql/data/custom-location/MyDatabase**_log.ldf files; ## Volumes