I am begrudgingly performing the soul-draining task of trying to use Microsoft SQL Server in a docker container, and am using the mssql-server-linux box provided by Microsoft. B
You're starting SQL Server in the background by using the &
. Because of this your sqlcmd will start immediately after starting SQL Server and not wait until it's ready to accept connections.
Usually you could solve this by using the double ampersand &&
. By using &&
the second command won't start before the first command completed succesfully. However, SQL Server is a long running proces so your second command will never start.
You can solve this by adding a sleep command after starting SQL Server.
/opt/mssql/bin/sqlservr & sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost etc.
The sqlcmd will execute 30 seconds after starting SQL Server. If SQL Server is still not available try to up the sleep time.
After the sqlcmd is finished the docker container will stop because there is no more foreground process. SQL Server is running the background and docker requires an active foreground process.
Add the wait
command to start waiting for the SQL Server process again.
/opt/mssql/bin/sqlservr & sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost etc. && wait
disclaimer I just started working with Linux and Docker myself so there might be better solutions.