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

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"

Related

Cannot connect to custom SQL Server docker image

we're currently using Windows containers and, as there are no official SQL Server images for Windows, we are building our own.
We've successfully created the image and by running via
docker run --name SQLServer2022preview -e ACCEPT_EULA=Y -e sa_password=PaSSw0rd -p 1433:1433 -d mycompany:winsrv1809-sql2022preview
I can connect and work as expected to localhost,1433. However, this image will act as a base and we need to build another image based on this on runtime. When doing so, is where it can't connect. We're getting this message
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server [2]. .
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
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 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; sqlcmd -i "C:\restore.sql"' returned a non-zero code: 1
This is the docker file
FROM mycompany:winsrv1809-sql2022preview
EXPOSE 1433
EXPOSE 4338
COPY ./databases/DB1.bak C:/
COPY ./HAS_Database/restore.sql C:/
RUN sqlcmd -i "C:\\restore.sql"
and restore.sql
RESTORE DATABASE DB1
FROM DISK = N'C:\DB1.bak'
WITH FILE = 1,
MOVE N'DB1_Imported'
TO N'C:\DB1.mdf',
MOVE N'DB1_log'
TO N'C:\DB1_log.ldf',
NOUNLOAD,
STATS = 5;
GO
There's no login errors in sql log looking into the container (which automatically closes after aprox 30 sec) so I guess that it may be relative to port forwarding. Can anyone help?
I tried port forwarding in the start step, with no luck. I'm a docker newbie

Which port should I use for SQL Server on Docker with runner in bitbucket?

I'm using a python-mssql image to restore a .bak file on Bitbucket. This is the yaml file that I create:
image: diegonachon/mssql_python_sqlcmd
env:
- ACCEPT_EULA = Y
- SA_PASSWORD = 5tr0ngP455
pipelines:
custom:
run:
- step:
name: restauration
runs-on:
- 'self.hosted'
size: 8x
script:
- pip install pysftp
- pip install pyodbc
- pip install psycopg2
- mkdir /bak_db/
- python dowload_bak_file.py
- python restore_file.py
In download_bak_file.py I download a .bak file that I'll restore. Then, in restore_db.py, I restore it with this code:
import os
os.system(''' sqlcmd -S localhost -U SA -Q'''
''' "Restore DATABASE data_base FROM DISK = '/bak_db/data_base.bak' \
WITH MOVE 'data_base' TO '/bak_db/data_base.mdf', \
MOVE 'data_base' TO '/bak_db/data_base.ldf' " \
-P 5tr0ngP455 ''')
But I get these errors:
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 0x2AF9.
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.
What I've seen, is a port problem, so I assume that I should change "localhost" with something else. Is it or I'm missing something?
Never use localhost in docker configs. In dockers, localhost is inside the docker container itself. If you want to connect to a database, you should use its service name or machine IP.

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.

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

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"

Resources