From a1dc1d8953ec18d652576ef262ed26958b7b7760 Mon Sep 17 00:00:00 2001 From: Bob Ward Date: Fri, 16 Nov 2018 17:48:51 -0600 Subject: [PATCH] dtc with docker examples --- samples/containers/dtc/README.md | 38 +++++++++++++++++++++++ samples/containers/dtc/docker-compose.yml | 32 +++++++++++++++++++ samples/containers/dtc/dockerdtc1.sh | 1 + samples/containers/dtc/dockerdtc2.sh | 1 + samples/containers/dtc/dtc1/Dockerfile | 6 ++++ samples/containers/dtc/dtc1/db-init.sh | 6 ++++ samples/containers/dtc/dtc1/db-init.sql | 11 +++++++ samples/containers/dtc/dtc1/entrypoint.sh | 5 +++ samples/containers/dtc/dtc2/Dockerfile | 6 ++++ samples/containers/dtc/dtc2/db-init.sh | 6 ++++ samples/containers/dtc/dtc2/db-init.sql | 8 +++++ samples/containers/dtc/dtc2/entrypoint.sh | 5 +++ samples/containers/dtc/dtcinsert.sql | 11 +++++++ samples/containers/dtc/linkedserver.sql | 4 +++ 14 files changed, 140 insertions(+) create mode 100644 samples/containers/dtc/README.md create mode 100644 samples/containers/dtc/docker-compose.yml create mode 100644 samples/containers/dtc/dockerdtc1.sh create mode 100644 samples/containers/dtc/dockerdtc2.sh create mode 100644 samples/containers/dtc/dtc1/Dockerfile create mode 100644 samples/containers/dtc/dtc1/db-init.sh create mode 100644 samples/containers/dtc/dtc1/db-init.sql create mode 100644 samples/containers/dtc/dtc1/entrypoint.sh create mode 100644 samples/containers/dtc/dtc2/Dockerfile create mode 100644 samples/containers/dtc/dtc2/db-init.sh create mode 100644 samples/containers/dtc/dtc2/db-init.sql create mode 100644 samples/containers/dtc/dtc2/entrypoint.sh create mode 100644 samples/containers/dtc/dtcinsert.sql create mode 100644 samples/containers/dtc/linkedserver.sql diff --git a/samples/containers/dtc/README.md b/samples/containers/dtc/README.md new file mode 100644 index 00000000..2364b81d --- /dev/null +++ b/samples/containers/dtc/README.md @@ -0,0 +1,38 @@ +## SQL Server Replication with Containers + +This demo uses docker-compose to start two SQL Server containers; one that acts as the publisher and distributor, and the other as the subscriber in a push snapshot configuration. + + +### How to Use + +1. Run the following command in this directory: + +``` +docker-compose up +``` +note: this will take approx. 2 min. + +In your terminal, you should see something like this +``` +db1 | Job 'DB1-Sales-SnapshotRepl-1' started successfully. +db1 | Creating Snapshot... +db1 | Job 'db1-Sales-SnapshotRepl-DB2-1' started successfully. +``` + +2. Connect to the subscriber listening on localhost,2600 and see that the Sales Database has a Customer table with data in it. +note: credentials are listed in the **docker-compose.yml** + +3. when you are done, clean up by running the following command +``` +docker-compose down +``` + + + +### How it Works + +1. Both SQL Server containers start with the environment variables specified in the docker-compose file. In this example, **db1** is the publisher/distributor and **db2** is the subscriber. +2. *db1/db-init.sh* and *db2/db-init.sh* waits for SQL Server to start up and run the *db-init.sql* scripts +3. *db1/db-init.sql* creates a *Sales* Database with *Customer* table and sample data, and proceeds by setting up snapshot replication. +4. *db2/db-init.sql* creates a *Sales* Database. +5. db1 starts replication jobs to push the snapshot to db2 \ No newline at end of file diff --git a/samples/containers/dtc/docker-compose.yml b/samples/containers/dtc/docker-compose.yml new file mode 100644 index 00000000..bad76cbb --- /dev/null +++ b/samples/containers/dtc/docker-compose.yml @@ -0,0 +1,32 @@ +version: "3" +services: + dtc1: + build: ./dtc1 + environment: + SA_PASSWORD: "Sql2019isfast" + ACCEPT_EULA: "Y" + MSSQL_AGENT_ENABLED: "true" + MSSQL_RPC_PORT: 135 + MSSQL_DTC_TCP_PORT: 51999 + ports: + - "1401:1433" + - "135:135" + - "51999:51999" + container_name: dtc1 + hostname: dtc1 + dtc2: + build: ./dtc2 + environment: + SA_PASSWORD: "Sql2019isfast" + ACCEPT_EULA: "Y" + MSSQL_AGENT_ENABLED: "true" + MSSQL_RPC_PORT: 135 + MSSQL_DTC_TCP_PORT: 51999 + ports: + - "1402:1433" + - "136:135" + - "51998:51999" + container_name: dtc2 + hostname: dtc2 + + diff --git a/samples/containers/dtc/dockerdtc1.sh b/samples/containers/dtc/dockerdtc1.sh new file mode 100644 index 00000000..066c134b --- /dev/null +++ b/samples/containers/dtc/dockerdtc1.sh @@ -0,0 +1 @@ +sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Sql2019isfast' -e 'MSSQL_RPC_PORT=135' -e 'MSSQL_DTC_TCP_PORT=51000' -p 1401:1433 -p 135:135 -p 51000:51000 --hostname dtc1 --network isolated_nw --name dtc1 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu diff --git a/samples/containers/dtc/dockerdtc2.sh b/samples/containers/dtc/dockerdtc2.sh new file mode 100644 index 00000000..37848380 --- /dev/null +++ b/samples/containers/dtc/dockerdtc2.sh @@ -0,0 +1 @@ +sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Sql2019isfast' -e 'MSSQL_RPC_PORT=135' -e 'MSSQL_DTC_TCP_PORT=51000' -p 1402:1433 -p 136:135 -p 51001:51000 --hostname dtc2 --network isolated_nw --name dtc2 -d mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu diff --git a/samples/containers/dtc/dtc1/Dockerfile b/samples/containers/dtc/dtc1/Dockerfile new file mode 100644 index 00000000..60965d2b --- /dev/null +++ b/samples/containers/dtc/dtc1/Dockerfile @@ -0,0 +1,6 @@ +FROM mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu + +COPY . / + +RUN chmod +x /db-init.sh +CMD /bin/bash ./entrypoint.sh \ No newline at end of file diff --git a/samples/containers/dtc/dtc1/db-init.sh b/samples/containers/dtc/dtc1/db-init.sh new file mode 100644 index 00000000..94117b2a --- /dev/null +++ b/samples/containers/dtc/dtc1/db-init.sh @@ -0,0 +1,6 @@ +#wait for the SQL Server to come up +sleep 25s + +echo "running set up script" +#run the setup script to create the DB and the schema in the DB +/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Sql2019isfast -d master -i db-init.sql diff --git a/samples/containers/dtc/dtc1/db-init.sql b/samples/containers/dtc/dtc1/db-init.sql new file mode 100644 index 00000000..ecded2c7 --- /dev/null +++ b/samples/containers/dtc/dtc1/db-init.sql @@ -0,0 +1,11 @@ +USE master +GO +EXEC master.dbo.sp_addlinkedserver + @server = N'dtc2', @srvproduct=N'SQL Server' +GO +CREATE DATABASE dtclinux1 +GO +USE [dtclinux1] +GO +CREATE TABLE dtctable (col1 int) +GO diff --git a/samples/containers/dtc/dtc1/entrypoint.sh b/samples/containers/dtc/dtc1/entrypoint.sh new file mode 100644 index 00000000..4a6e4fe6 --- /dev/null +++ b/samples/containers/dtc/dtc1/entrypoint.sh @@ -0,0 +1,5 @@ +#start SQL Server, start the script to create/setup the DB +#You need a non-terminating process to keep the container alive. +#In a series of commands separated by single ampersands the commands to the left of the right-most ampersand are run in the background. +#So - if you are executing a series of commands simultaneously using single ampersands, the command at the right-most position needs to be non-terminating + /db-init.sh & /opt/mssql/bin/sqlservr \ No newline at end of file diff --git a/samples/containers/dtc/dtc2/Dockerfile b/samples/containers/dtc/dtc2/Dockerfile new file mode 100644 index 00000000..60965d2b --- /dev/null +++ b/samples/containers/dtc/dtc2/Dockerfile @@ -0,0 +1,6 @@ +FROM mcr.microsoft.com/mssql/server:vNext-CTP2.0-ubuntu + +COPY . / + +RUN chmod +x /db-init.sh +CMD /bin/bash ./entrypoint.sh \ No newline at end of file diff --git a/samples/containers/dtc/dtc2/db-init.sh b/samples/containers/dtc/dtc2/db-init.sh new file mode 100644 index 00000000..a579e6cb --- /dev/null +++ b/samples/containers/dtc/dtc2/db-init.sh @@ -0,0 +1,6 @@ +#wait for the SQL Server to come up +sleep 20s + +echo "running set up script" +#run the setup script to create the DB and the schema in the DB +/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Sql2019isfast -d master -i db-init.sql diff --git a/samples/containers/dtc/dtc2/db-init.sql b/samples/containers/dtc/dtc2/db-init.sql new file mode 100644 index 00000000..6b6ef1f7 --- /dev/null +++ b/samples/containers/dtc/dtc2/db-init.sql @@ -0,0 +1,8 @@ +USE master +go +CREATE DATABASE dtclinux2 +GO +USE dtclinux2 +GO +CREATE TABLE dtctable (col1 int) +GO diff --git a/samples/containers/dtc/dtc2/entrypoint.sh b/samples/containers/dtc/dtc2/entrypoint.sh new file mode 100644 index 00000000..4a6e4fe6 --- /dev/null +++ b/samples/containers/dtc/dtc2/entrypoint.sh @@ -0,0 +1,5 @@ +#start SQL Server, start the script to create/setup the DB +#You need a non-terminating process to keep the container alive. +#In a series of commands separated by single ampersands the commands to the left of the right-most ampersand are run in the background. +#So - if you are executing a series of commands simultaneously using single ampersands, the command at the right-most position needs to be non-terminating + /db-init.sh & /opt/mssql/bin/sqlservr \ No newline at end of file diff --git a/samples/containers/dtc/dtcinsert.sql b/samples/containers/dtc/dtcinsert.sql new file mode 100644 index 00000000..03825fe8 --- /dev/null +++ b/samples/containers/dtc/dtcinsert.sql @@ -0,0 +1,11 @@ +set xact_abort on +go +begin distributed transaction +go +use dtclinux1 +go +insert into dtctable values (1) +insert into dtc2.dtclinux2.dbo.dtctable values (1) +go +commit tran +go diff --git a/samples/containers/dtc/linkedserver.sql b/samples/containers/dtc/linkedserver.sql new file mode 100644 index 00000000..50967754 --- /dev/null +++ b/samples/containers/dtc/linkedserver.sql @@ -0,0 +1,4 @@ +USE master +GO +EXEC sp_addlinkedserver N'dtc2',N'SQL Server' +GO