0

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"

enter image description here

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 sqlcmd to give SQL Server time 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?

1 Answer 1

1

You are just building an image, the database process is not running. That is why sqlcmd is not able to connect.

The database is started when you run a container out of your image, not during image building stage

Microsoft SQL Docker image documentation links to an example that might be what you are looking for

You can also use the tools in an entrypoint.sh script to do things like create databases or logins, attach databases, import data, or other setup tasks. See this example of using an entrypoint.sh script to create a database and schema and bcp in some data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.