Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Adding functionality to Attach database #3

Open
wants to merge 2 commits into
base: 2019
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions docker-entrypoint-initdb.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -57,6 +57,13 @@ 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;

#### 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