I am trying to setup a Microsoft SQL Server from a Docker image and load a script file that creates a database and adds a couple of file groups to that database.
The dockerfile looks like this:
# Get MSSQL image
FROM mcr.microsoft.com/mssql/server:2017-CU24-ubuntu-16.04
# Set arguments (must be after FROM)
ARG SA_PASSWORD=SecretComplexPassword1234!
# Set environment variables for MSSQL
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=$SA_PASSWORD
# Setup sqlcmd symlink
RUN ln -sfn /opt/mssql-tools/bin/sqlcmd /usr/bin/sqlcmd
# Setup Database
COPY ./init_script_core.sql /tmp
RUN sqlcmd -S localhost -U SA -P $SA_PASSWORD -i /tmp/init_script_core.sql
When the sqlcmd step is invoked it takes some time (around 8 seconds) and then returns this error:
> [4/4] RUN sqlcmd -S localhost -U SA -P SecretComplexPassword1234! -i /tmp/init_script_core.sql:
#8 8.720 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
#8 8.720 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
#8 8.720 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..
I first removed the sqlcmd step from the dockerfile to make sure that I could build and run the dockerfile without any problem. Then I executed the sqlcmd from PowerShell with the only difference being the path to the script file. I then verified that the script had actually been executed and I could see the the differences by connecting to the SQL Server using SSMS on my local (Windows) machine:
PS C:\github> docker build -t docker-core-test .
PS C:\github> docker run -d -i -p 11433:1433 docker-core-test
08c831bf99533301c0225eae9ea6433006dba0f052666782ac42de140784cc10
PS C:\GitHub> sqlcmd -S localhost,11433 -U SA -P SecretComplexPassword1234! -i "C:\GitHub\init_script_core.sql"
As there doesn't seem to be any wrong with the actual setup of SQL Server or the script I started to Google around a little bit and found these solutions that worked for other people but unfortunately not for me:
- Add a 10 second timer before
sqlcmdto giveSQL Servertime to start - Add port to
localhost -> localhost,1433 - Change
localhost 127.0.0.1 - Add port to
127.0.0.1 -> 127.0.0.1,1433
Question: Why is sqlcmd timing out when invoked while building my Docker image?
