Score:0

Run SQL script on Ms SQL Server docker container creation

in flag

I want to run sql script on MS SQL container setup.

If I do everything separately all works fine:

  1. Run container docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Password" -p 1434:1433 --name ms-sql -d mcr.microsoft.com/mssql/server:2019-latest

  2. Connect to container docker exec -it ms-sql "bash"

  3. Run SQL /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Password' -Q 'create database TestDB12'

But if try to do same at container creation

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Password" -p 1434:1433 --name ms-sql -d mcr.microsoft.com/mssql/server:2019-latest sh -c "/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Password' -Q 'create database TestDB12'"

sqlcmd fail to login with error

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Score:1
in flag

By providing sh -c "/opt/mssql-tools/bin/sqlcmd ... to the run command you are replacing the command that starts the SQL server in the container with the sqlcmd command.

The server is not started inside the container. You need to start it first and run your command after it has finished the startup.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.