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

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.

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

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"

DotNetCore Docker container could not connect to mssql

I have containerized our DotNetCore application.
Our application code connects to different SQL Server instances
We are observing a weird error that our Application is not able to connect to a particular SQL Server instance.
Any connection trying to hit the SQL will go forever in Sending form.
I tried the below to identify the error.
Debug my Docker container locally and install SQL CMD to check if I am able to connect to SQL Server
Below is the error that is occurring
root#aabcd:/app# sqlcmd -S abcinstnacename,3431 -U username -p -Q "SELECT ##VERSION"
Password:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
SQL Server Version that is causing the error
Microsoft SQL Server 2016 (SP2-CU2-GDR) (KB4458621) - 13.0.5201.2 (X64)
In Docker file below are the base images I am using, will that cause any issue ?
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
Also tried adding below line in my Dockerfile with no luck!
RUN sed -i '/^ssl_conf = ssl_sect$/s/^/#/' /etc/ssl/openssl.cnf
Please share your thoughts on this

How to connect to SQL Server Developer (windows edition) in a container on Win10 from outside the container

I got the docker image for SQL Server Developer edition and started it up:
PS C:\WINDOWS\system32> docker ps -aa
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81347866fc2b microsoft/mssql-server-windows-developer "powershell -Command…" About an hour ago Up About an hour (healthy) 0.0.0.0:1433->3341/tcp trusting_mccarthy
PS C:\WINDOWS\system32>
It's listening on port 3341 as you can see. the IP is:
PS C:\WINDOWS\system32> docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 8
172.17.234.173
HOwever I cannot connect to it from the host:
PS C:\WINDOWS\system32> sqlcmd -S 172.17.234.173,3341
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: An attempt was made to access a socket in a way forbidden by its access permissions.
.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 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..
Must be doing something wrong. any hints?
PS: I can't connect to 1433 either:
PS P:\> sqlcmd -S 172.17.234.173,1433
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: An attempt was made to access a socket in a way forbidden by its access permissions.
.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 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..
PS P:\>
Docker Run command used:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong!Passw0rd>" `
-p 1433:1433 --name sql1 `
-d mcr.microsoft.com/mssql-server-windows-developer
note I've started with 1433:3341 as well. No joy either way

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