Docker: How to run a sql script in SQL Server Dockerfile - sql-server

I am trying to build a SQL Server container and then run some SQL scripts to add a user and make the schema by using docker build . and later docker-compose build -> docker-compose up
FROM mcr.microsoft.com/mssql/server
COPY all.sql all.sql
COPY auto-setup-db.sql auto-setup-db.sql
RUN /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql
this line^ fails with:
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..
The command '/bin/sh -c /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql' returned a non-zero code: 1
And I'm not sure what I'm doing wrong, does anyone have an idea?
EDIT: this is the relevant part of my docker-compose file:
version: '3'
services:
mssql:
image: mssql
build: ./e2e/mssql
container_name: mssql
environment:
SA_PASSWORD: "myPassword"
ACCEPT_EULA: "Y"
EDIT 2:
Running docker-compose up gives a lot of output from SQL Server ex.
2019-08-29 17:05:13.02 spid8s Starting up database 'tempdb'.
but I need to find a way to run the schema script in there so I thought docker-build would be the easiest place. I'm very new to docker

Trying adding double quotes around the file path and use the full path. Please see the example.
.
.
.
EXPOSE 1433
RUN mkdir -p /var/opt/mssql/database
WORKDIR /var/opt/mssql/database
COPY auto-setup-db.sql .
CMD /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P myPassword -i "/var/opt/mssql/database/auto-setup-db.sql" -o "/var/opt/mssql/database/auto-setup-db.log"

Related

SQL Server inside a docker container on Linux: Login from outside not possible

I have here a SQL Server Docker container. I have created the container with this command:
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=SA.123456" -u 0:0 -p 1433:1433 --name sql1 --hostname sql1 -d mcr.microsoft.com/mssql/server:2022-latest
I can start the container
docker start sql1
and I can open a SQL console inside of the container:
docker exec -it sql1 "bash"
root#sql1:/# /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1,1433 -U SA
Password:
So far, so good. But I want to open a SQL console from outside the container. To do that I run sqlcmd from my linux machine directly, but I get following error:
/> /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1,1433 -U SA
Password:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Timeout error [258].
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Unable to complete login process due to delay in prelogin response.
If I try to connect to 127.0.0.1:1433 I get a connection (and I have also checked that the connection is refused if I stop the container)
/> telnet 127.0.0.1 1433
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
So I'm sure that the connection by self can be established. But why I get the error "Unable to complete login process due to delay in prelogin response."?

Connect to SQL Server inside Docker image (Linux) whilst in build

I have a simple docker-compose file which builds a SQL Server database, restores a .bak file.
Now, if I run this independently - i.e. wait for the image to build and exclude the run.sh and run it with docker exec it sql-server-db /opt/mssql-tools/bin/sqlcmd it works no problem.
However, if I run this inside the build process I get
#0 8.435 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
#0 8.435 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
#0 8.435 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.
Where is my error?
docker-compose
version: '3.9'
services:
sql-server-db:
container_name: sql-svr-db
build:
context: .
dockerfile: docker/mssql.Dockerfile
ports:
- "5433:1433"
volumes:
- /drive:/var/opt/mssql
mssql.Dockerfile
FROM 'mcr.microsoft.com/mssql/server'
ENV SA_PASSWORD=Passw0rd!
ENV ACCEPT_EULA="Y"
USER root
WORKDIR /app/
RUN mkdir -p /var/opt/mssql/backup
COPY /drive/db.bak /var/opt/mssql/backup
ENTRYPOINT [ "/app/run.sh" ]
.run.sh
#!/bin/bash
echo "waiting for sql server to warm up ..."
sleep 45
echo "restoring database... from BAK file"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA \
-P 'Passw0rd!' \
-Q 'RESTORE DATABASE MYDB FROM DISK = "/var/opt/mssql/backup/db.bak" WITH MOVE "db_DATA" TO "/var/opt/mssql/data/db.mdf", MOVE "db_Log" TO "/var/opt/mssql/data/db.ldf"'
echo "database restored"

SQL Server Linux Change COLLATION

I have a SQL server which is triggered inside docker container from gitlab. Following yaml file does that :
services:
- name: mcr.microsoft.com/mssql/server:2019-latest
alias: mssql
variables:
MSSQL_HOST: microsoft-mssql-server-linux
ACCEPT_EULA: Y
MSSQL_COLLATION: Latin1_General_CS_AS
SA_PASSWORD: yourStrong(!)Password
Now when i am starting this pipeline its giving error in running the sql script saying that login failed for user SA . Error is as :
$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P
"yourStrong(!)Password" -i Scripts/DBScript.sql Sqlcmd: Error:
Microsoft ODBC Driver 17 for SQL Server : Login failed for user 'SA'..
Cleaning up file based variables ERROR: Job failed: command terminated
with exit code 1
This error gets resolved once i remove the collation "MSSQL_COLLATION:Latin1_General_CS_AS" from the yaml file.
This explains that i am not able to change MSSQL Serve collation.
Note: Container spawned by Gitlab is Linux Container which is also installing docker image of "mcr.microsoft.com/mssql/server:2019-latest".
Any idea how to change Collation level at MS SQL server level.

Docker SQL Server 2017 on linux connection issue

My Docker linux SQL Server is not working today at my machine.
I am not sure if this is firewall (I have off them all), or Docker settings (as I just upgraded to the latest Docker version), or a Docker SQL Server issue (but this was working fine on the same machine earlier).
Could anyone help?
I have tried using bash,
/opt/mssql-tools/bin/sqlcmd -S localhost,8010 -U SA -P Test123!
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..
The docker compose file
version: '3'
services:
mssql:
network_mode: lsvc
image: microsoft/mssql-server-linux:2017-latest
container_name: mssql
hostname: mssql
volumes:
- ./.db:/var/opt/mssql/
- /var/opt/mssql/data
- ./sqlinit.sql:/scripts/sqlinit.sql
ports:
- 8010:1433
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=Test123!
command:
- /bin/bash
- -c
- |
# Launch MSSQL and send to background
/opt/mssql/bin/sqlservr &
# Wait for it to be available
echo "Waiting for MS SQL to be available"
/opt/mssql-tools/bin/sqlcmd -l 30 -S mssql -h-1 -V1 -U sa -P Test123! -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , ##servername"
is_up=$$?
while [ $$is_up -ne 0 ] ; do
echo -e $$(date)
/opt/mssql-tools/bin/sqlcmd -l 30 -S mssql -h-1 -V1 -U sa -P Test123! -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , ##servername"
is_up=$$?
sleep 1
done
# Run every script in /scripts
# TODO set a flag so that this is only done once on creation,
# and not every time the container runs
#for foo in /scripts/*.sql
/opt/mssql-tools/bin/sqlcmd -S mssql -U sa -P Test123! -l 30 -e -i /scripts/sqlinit.sql
#done
# So that the container doesn't shut down, sleep this thread
sleep infinity
I would suspect that the mssql instance had failed to start.
Looking at your docker file when the server fail to start it runs into in infinite loop. I would advice you to limit the number of retries on the loop so you will have an indication that the server is failing on startup.
Or better consider the use of the HEALTHCHECK option and not the loop script
see https://blog.couchbase.com/docker-health-check-keeping-containers-healthy/
To troubleshooting the problem try
docker logs <mssql-container-id>
if this doesn't provide enough information try connecting to the container as this was a simple machine.
docker exec -it <mssql-container-id> bash
Look for errors in /var/opt/mssql/log/errorlog
See https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-troubleshooting-guide?view=sql-server-ver15
i found the root cause, it is not due to docker mssql linux
but the latest Docker Desktop Community 2.2.0.0
after uninstall it then downgrade to old version, Docker Desktop Community 2.1.0.5.
it is working now, connect to mssql success.
not sure what is new in latest docker desktop, anyway...i will use old version now.
also one important point to take note, the ".db" folder (windows path where the yml file reside), must deleted before build the docker image.

sqlcmd works in CMD and not in bash

I am having quite a few problems making MSSQL drivers work in Ubuntu. I have followed the following tutorial to make sqlcmd work in Ubuntu 16.04.
# In CMD:
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
1> select name from sys.databases
2> go
After installing the same tool in Ubuntu, it seems to work, but it timeouts when attempting to connect to the same database:
# In Ubuntu bash
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
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 0x2AFA.
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 have tried to change configurations of the database, such that the port is static at 1433, but still no luck.
Do you have any suggestions?
I have fixed the problem!
1. Install MSSQL drivers on Ubuntu
Follow this tutorial.
2. Ensure your port on the database is static
Follow this tutorial to set up a static port.
3. Identify the IP adress of your database
I had to call the following code on the database to get the ip-adress: local_net_address.
SELECT
+ CONNECTIONPROPERTY('net_transport') AS net_transport,
+ CONNECTIONPROPERTY('protocol_type') AS protocol_type,
+ CONNECTIONPROPERTY('auth_scheme') AS auth_scheme,
+ CONNECTIONPROPERTY('local_net_address') AS local_net_address,
+ CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port,
+ CONNECTIONPROPERTY('client_net_address') AS client_net_address
4. Connect to database (in Ubuntu bash)
Here are two examples with netcat and sqlcmd.
# Using MSSQL tool
sqlcmd -S my_server_ip_adress//my_server_name,my_port -U my_username -P my_password -d my_database
# Using netcat
nc -z -v -w5 my_server_ip_adress my_port

Resources