@@ -34,37 +34,63 @@ steps:
3434 docker pull mcr.microsoft.com/mssql/server:2022-latest
3535
3636 # Password for the SA user (required)
37- MSSQL_SA_PW=${{parameters.password }}
37+ MSSQL_SA_PW=${{ parameters.password }}
3838
3939 docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=$MSSQL_SA_PW" -p 1433:1433 -p 1434:1434 --name sql1 --hostname sql1 -d mcr.microsoft.com/mssql/server:2022-latest
4040
4141 sleep 5
4242
4343 docker ps -a
4444
45- # Connect to server and get the version:
46- counter=1
47- errstatus=1
48- while [ $counter -le 20 ] && [ $errstatus = 1 ]
45+ # Connect to the SQL Server container and get its version.
46+ #
47+ # It can take a while for the docker container to start listening and be
48+ # ready for connections, so we will wait for up to 2 minutes, checking every
49+ # 3 seconds.
50+
51+ # Wait 3 seconds between attempts.
52+ delay=3
53+
54+ # Try up to 40 times (2 minutes) to connect.
55+ maxAttempts=40
56+
57+ # Attempt counter.
58+ attempt=1
59+
60+ # Flag to indicate when SQL Server is ready to accept connections.
61+ ready=0
62+
63+ while [ $attempt -le $maxAttempts ]
4964 do
50- echo Waiting for SQL Server to start...
51- sleep 3
52- sqlcmd -S 0.0.0.0 -No -U sa -P $MSSQL_SA_PW -Q "SELECT @@VERSION" 2>$SQLCMD_ERRORS
53- errstatus=$?
54- ((counter++))
65+ echo "Waiting for SQL Server to start (attempt #$attempt of $maxAttempts)..."
66+
67+ sqlcmd -S 127.0.0.1 -No -U sa -P $MSSQL_SA_PW -Q "SELECT @@VERSION" >> $SQLCMD_ERRORS 2>&1
68+
69+ # If the command was successful, then the SQL Server is ready.
70+ if [ $? -eq 0 ]; then
71+ ready=1
72+ break
73+ fi
74+
75+ # Increment the attempt counter.
76+ ((attempt++))
77+
78+ # Wait before trying again.
79+ sleep $delay
5580 done
5681
57- # Display error if connection failed:
58- if [ $errstatus = 1 ]
82+ # Is the SQL Server ready?
83+ if [ $ready -eq 0 ]
5984 then
60- echo Cannot connect to SQL Server, installation aborted
85+ # No, so report the error(s) and exit.
86+ echo Cannot connect to SQL Server; installation aborted; errors were:
6187 cat $SQLCMD_ERRORS
6288 rm -f $SQLCMD_ERRORS
63- exit $errstatus
64- else
65- rm -f $SQLCMD_ERRORS
89+ exit 1
6690 fi
6791
92+ rm -f $SQLCMD_ERRORS
93+
6894 echo "Use sqlcmd to show which IP addresses are being listened on..."
6995 echo 0.0.0.0
7096 sqlcmd -S 0.0.0.0 -No -U sa -P $MSSQL_SA_PW -Q "SELECT @@VERSION" -l 2
@@ -78,7 +104,7 @@ steps:
78104 sqlcmd -No -U sa -P $MSSQL_SA_PW -Q "SELECT @@VERSION" -l 2
79105
80106 echo "Configuring Dedicated Administer Connections to allow remote connections..."
81- sqlcmd -S 0 .0.0.0 -No -U sa -P $MSSQL_SA_PW -Q "sp_configure 'remote admin connections', 1; RECONFIGURE;"
107+ sqlcmd -S 127 .0.0.1 -No -U sa -P $MSSQL_SA_PW -Q "sp_configure 'remote admin connections', 1; RECONFIGURE;"
82108 if [ $? = 1 ]
83109 then
84110 echo "Error configuring DAC for remote access."
0 commit comments