Bitbucket pipeline sql server database set port - sql-server

I have a bitbucket pipeline that must execute django unittests. Therefore, I need a test database which should be a SQL SERVER datbase.
The pipeline looks like this:
# This is a sample build configuration for Python.
# Check our guides at https://confluence.atlassian.com/x/x4UWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: python:3.7.3
pipelines:
branches:
master:
- step:
name: Setup sql
image: fabiang/sqlcmd
script:
- sqlcmd -S localhost -U sa -P $DB_PASSWORD
services:
- sqlserver
- step:
name: Run tests
caches:
- pip
script: # Modify the commands below to build your repository.
- python3 -m venv my_env
- source my_env/bin/activate
- apt-get update && apt-get install
- pip3 install -r req-dev.txt
- python3 manage.py test
- step:
name: Linter
script: # Modify the commands below to build your repository.
- pip3 install flake8
- flake8 --exclude=__init__.py migrations/
definitions:
services:
sqlserver:
image: mcr.microsoft.com/mssql/server:2017-latest
variables:
ACCEPT_EULA: Y
SA_PASSWORD: $DB_PASSWORD
And everytime when I run the pipeline I get:
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 0x2726.
I tried to do it locally but then it only work when I defined a port with the -p tag:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong!' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest
How can I make the pipeline work? (probably defining a port but how?)
UPDATE:
On the sqlserver tab in the result section is the following error shown:

I think the problem is when you call the script - sqlcmd -S localhost -U sa -P $DB_PASSWORD because your sqlserver is not yet completly initialized.
Try to put a sleep 10 before the command and the best is to add an error case if command fail sleep 5 and retry again.

Related

Using Docker to Create 3 containers: Two SQL Servers and 1 Python Image

I am trying to create 3 containers and have them run all on the same network, but I am struggling with my Python Program to establish a successful connection to a SQL server on any of these containers.
To describe my current work flow:
I have one Microsoft SQL Image Container called server_1.
I have another Microsoft SQL Image Container called server_2.
Finally, I have another container called python-connect containing a Python Image called python-file.
To show you how I created each of these containers this is what I did.
First, I created a network called databases-net as follows:
docker network create --driver bridge databases-net
Next, I proceeded to create two containers with SQL servers:
docker run -dit -d --name server_1 --network databases-net -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=StrongPassword123#' -p 1433:1433 mcr.microsoft.com/mssql/server:2019-latest
docker run -dit -d --name server_1 --network databases-net -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=StrongPassword123#' -p 1002:1433 mcr.microsoft.com/mssql/server:2019-latest
Then, I created a DockerFile as shown below for my Python Program:
FROM python:3.9.2-slim-buster
ADD dbconnTest.py .
RUN apt-get update \
&& apt-get -y install gcc \
&& apt-get -y install g++ \
&& apt-get -y install unixodbc unixodbc-dev \
&& apt-get clean
RUN pip install pandas pyodbc
CMD [ "python", "./dbconnTest.py" ]
This is the python program I am using. Currently, I am only trying to access the data inside the server_1 container:
import pandas as pd
import pyodbc
cnxn = pyodbc.connect(
'DRIVER={ODBC Driver 17 for SQL Server}' +
';SERVER=' + 'localhost, 1433' + ';UID=' + 'sa' +
';PWD=' + 'StrongPassword123#' +
';database=' + 'tempdb')
df = pd.read_sql('SELECT * FROM Addresses', cnxn)
cnxn.commit()
cnxn.close()
print(df)
To build this image and running it on a container I ran:
docker build -t python-file .
docker run -dit --name python-connect --network databases-net python-file
Once it was run, I got this error:
Traceback (most recent call last):
File "//./dbconnTest.py", line 4, in <module>
cnxn = pyodbc.connect(
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
It seems like the python program is not able to access any of the SQL containers I created although they are on the same network. I am quite new to Docker and after looking up many tutorials, I did not see anyone try doing this. Is it even possible to access SQL servers on containers separate from the container the python program is on? If so, how do you do so?
I've been stuck on this for a week, and would really appreciate all the help I can get on this effort. Thanks!

Gitlab with SQL Server as database source configuration handling

I have a self hosted gitlab on ubuntu machine. I configure a linux container for it to run runner. Now, I am trying to write a configuration for my dotnet project to run unit test on this setup.
I get configuration to run dotnet application without database, and only part I got stuck is that I cannot get Database to load or connect through my test environment.
I get SQL Server linux container to run as service (I am guessing it is running). But I am not sure how I can load my database to it. I know I can do that using Docker Run. But I cannot figure it out how to run it here.
When I try to run "mssql-tools" as service I cannot get it's command to run as it is not install by default in dotnet image.
Here is my file.
image: microsoft/dotnet:latest
variables:
ACCEPT_EULA: Y
SA_PASSWORD: my_secure_password
MSSQL_PID: Developer
stages:
- test
before_script:
- "cd Source"
- "dotnet restore"
test:
stage: test
services:
- mcr.microsoft.com/mssql/server:2017-latest
- mcr.microsoft.com/mssql-tools
script:
- "cd ../Database"
- "docker run -it mcr.microsoft.com/mssql-tools"
- "sqlcmd -S . -U SA -P my_secure_password -i testdata_structure.sql"
- "exit"
- "cd ../Source"
- "dotnet build"
- "dotnet test"
"sqlcmd -S . -U SA -P my_secure_password -i testdata_structure.sql this command won't work in this setup as sqlcmd is not installed, but is one of service. I don't want to make a new image that has all pre-install. But use available stuff to work.
Not, sure if I am able to explain my issue and knowledge here. I am new, but I am reading and changing configuration from 2 days. I can get Linux based SQL Server to run with my app from local docker commands and stuff, but on Gitlab to run Unit Test I cannot get database to restore/get running and connect to application.
GitLab Services does not install commands or apps inside your container job, instead a Service is another container that is usually run in parallel to offer infrastructure services such as databases, cache, queues, etc.
if you want to have sqlcmd inside your container you must install it:
This is an extract from my pipeline, in this case my container is based on Alpine but you can find more ways here: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
before_script:
- apk add curl
- apk add --no-cache gnupg
- curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.7.2.1-1_amd64.sig
- curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.7.1.1-1_amd64.sig
- curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.7.2.1-1_amd64.apk
- curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.7.1.1-1_amd64.apk
- curl https://packages.microsoft.com/keys/microsoft.asc | gpg --import -
- gpg --verify msodbcsql17_17.7.2.1-1_amd64.sig msodbcsql17_17.7.2.1-1_amd64.apk
- gpg --verify mssql-tools_17.7.1.1-1_amd64.sig mssql-tools_17.7.1.1-1_amd64.apk
- apk add --allow-untrusted msodbcsql17_17.7.2.1-1_amd64.apk
- apk add --allow-untrusted mssql-tools_17.7.1.1-1_amd64.apk
script:
- /opt/mssql-tools/bin/sqlcmd -S $DBC_SERVER -U $DBC_USER -P $DBC_PASSWORD -q "USE myTestDb; CREATE TABLE testGitlab (id int); SELECT * FROM testGitLab"
I end up using my custom Docker Image that has dotnetcore and Sqlcmd installed in it, I can use MsSQL Server as Service in gitlab configuration. (have to define SQL Server' hostname, as IP in same range as my server).
Not an idle answer, but workaround for me.

Login fails for SA sql server linux docker

I try to use sql server on docker, linux. I start the container like this:
docker run -d -p 1433:1433 -e sa_password="12345qwerASDF" -e ACCEPT_EULA=Y --name sql-server --hostname sql-server microsoft/mssql-server-linux:2017-latest
When I try to connect, all I get is "Login failed for user 'sa'"
Tried with different password, with and without double and single quotes...
Finally I got it to work:
docker run --name sqlserver --hostname sqlserver -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=1StrongPwd!!" -p 1433:1433 -d microsoft/mssql-server-linux:2017-latest
I guess it should be MSSQL_SA_PASSWORD and not SA_PASSWORD
The documentation isn't clear in that part...
Your password 12345qwerASDF doesn't meet complexity requirements. Try adding a non-alphanumeric characters such as exclamation point (!).
To check for errors, run: docker logs ID (where ID is container ID from docker ps).
For docker-compose file
i was facing problem with sa user login failed i fix it by adding
depends_on:
- db
here's my full version of docker-compose file
version: '3.1'
services:
colour-api:
build: .
environment:
DBServer: "ms-sql-server"
ports:
- "8080:80"
depends_on:
- ms-sql-server
ms-sql-server:
image: mcr.microsoft.com/mssql/server
environment:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: "Pa55w0rd2019"
ports:
- "1444:1433"
Follow this link for more details
https://docs.docker.com/compose/aspnet-mssql-compose/
I got the same error. Stop the sql server services running in your local. Then You can login to sql server(linux) via docker(dbeaver)
1-) I got the error
2-) Stop the sql server services running in your local
3-) Then You can login to sql server(linux) via docker(dbeaver)
I hope it was helpful
Try this :
docker run -e "ACCEPT_EULA=Y" -e "sa_password=12345qwerASDF" -p 1433:1433 --name sql-server --hostname sql-server -d microsoft/mssql-server-linux:2017-latest
IMPORTANT NOTE: If you are using PowerShell on Windows to run these
commands use double quotes instead of single quotes.
Source : https://hub.docker.com/r/microsoft/mssql-server-linux/
I solved my problem with this notes
In formal doc used SA_PASSWORD !!!, but MSSQL_SA_PASSWORD is true (Microsoft team, what are doing?!!!!)
As a docker "[Configuration/Requirements for Microsoft SQL Server][1]" said: " A strong system administrator (SA) password: At least 8 characters including uppercase, lowercase letters, base-10 digits and/or non-alphanumeric symbols."
In Linux, use single quote for define password .
Be sure, before you run "docker run .." , the volume of "data" which you mount from host to container must be clean (if you have it!).
Here is a sample of “docker run” for creation Microsoft SQL Server container.
docker run
-e ACCEPT_EULA=Y
-e MSSQL_SA_PASSWORD='Mssql!Passw0rd'
-e MSSQL_DATA_DIR=/var/opt/mssql/data
-e MSSQL_PID='Developer'
-e MSSQL_TCP_PORT=1433
-p 1433:1433
-v /var/opt/mssql/data:/var/opt/mssql/data
-v /var/opt/mssql/log:/var/opt/mssql/log
-v /var/opt/mssql/secrets:/var/opt/mssql/secrets
--name mssql_2017
-d mcr.microsoft.com/mssql/server:2017-latest
for checking an instance of SQL Server ...
docker exec -it mssql_2017 /bin/bash
cd /opt/mssql-tools/bin/
./sqlcmd -S localhost -U SA -P 'Mssql!Passw0rd'
select ##version
go
I hope this hints can help you.
The docker volume used for the mssql service could be corrupted.
In a docker setup using mcr.microsoft.com/mssql/server:2019-latest
I have encountered a similar problem. Although I had no problem
connecting to the server from a container built from the command line the issue persisted for the docker container.
Removing the volume and recreating the container did the trick.
docker run --name SQLServer -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=“Yourpassword” -e MSSQL_PID="Express" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
Same thing happened to me but the problem was I had a windows desktop version of MSSQL and I forgot to stop the service before connecting to the docker version.

Create SQL Server database from a script in docker

Simple question I hope. I cannot find anything anywhere.
How do you create a database in a Microsoft SQL Server Docker container?
Dockerfile:
I am looking at the following Dockerfile:
FROM microsoft/mssql-server-windows-developer:latest
ENV sa_password ab873jouehxaAGR
WORKDIR /
COPY /db-scripts/create-db.sql .
# here be the existing run commnd in the image microsoft/mssql-server-windows-developer:latest
#CMD ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';", ".\\start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs \\\"$env:attach_dbs\\\" -Verbose" ]
RUN (sqlcmd -S localhost -U SA -P ab873jouehxaAGR -i create-db.sql)
docker-compose.yml:
I have put together the following docker compose file:
version: '3.4'
services:
sql.data:
image: ${DOCKER_REGISTRY}myfirst-mssql:latest
container_name: myfirst-mssql_container
build:
context: .
dockerfile: ./Dockerfile
environment:
SA_PASSWORD: ab873jouehxaAGR
ACCEPT_EULA: Y
Bringing it together
I am running the command docker-compose up against the above. And assuming create-db.sql file will simply create a database which is not relevant here to keep things minimal.
Errors
The error I get above is that the login for SA is invalid when it runs the .sql script:
Step 7/7 : RUN (sqlcmd -S localhost -U SA -P ab873jouehxaAGR -i create-db.sql)
---> Running in 2ac5644d0bd9
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'SA'..
It looks like this runs before the password has been changed to ab873jouehxaAGR which typically looks like the command from mssql-server-windows-developer:latest.json from inspecting the image in vscode - \start -sa_password $env:sa_password -ACCEPT_EULA $env:ACCEPT_EULA -attach_dbs actually does.
Environment
I am running docker Docker version 18.06.1-ce, build e68fc7a on Windows 10.
Attach or script
I am not specifying attaching a database using the environment variable attach_dbs of which I see in many examples.
I am trying to find a best practice for managing a sql container from a point of view of end to end testing and a lot of articles seem to not cover the data aspect part - ie Development Workflow
I would be interested to hear in comments thoughts on these two approaches in the Docker world.
using following commands can solve your problem
docker-compose up --build -d
version: '3.4'
services:
sql.data:
image: ${DOCKER_REGISTRY}myfirst-mssql:latest
container_name: myfirst-mssql_container
environment:
SA_PASSWORD: ab873jouehxaAGR
ACCEPT_EULA: Y
and after that:
docker exec myfirst-mssql_container sqlcmd
-d master
-S localhost
-U "sa"
-P "ab873jouehxaAGR"
-Q 'select 1'

Unable to connect to remote SQL server from container

I'm trying to connect to my remote SQL server from a docker container hosted on my computer.
But are just reciving the following error:
A network-related or in stance-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 i f SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.. Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired.
But if I try to connect to my SQL server from SQL server management studio on the host machine everything works properly.
Also note that 2 weeks ago everything also worked inside the docker container.
Here is my docker compose file and the docker file which has the SQL driver installed:
Compose:
version: '3'
services:
nginx:
image: nginx:1.10
volumes:
- ./:/var/www
- .docker/nginx/vhost.conf:/etc/nginx/conf.d/default.conf
ports:
- ${DOCKER_IP}80:80
links:
- php
networks:
- app-net
php:
build:
context: ./
dockerfile: .docker/php/DockerFile
volumes:
- ./:/var/www
networks:
- app-net
networks:
app-net:
driver: bridge
Docker file
FROM phpdockerio/php71-fpm:latest
# Install selected extensions and other stuff
RUN export DEBIAN_FRONTEND=noninteractive && \
apt-get update && apt-get -y --no-install-recommends install \
php7.1-mysql \
php7.1-mbstring \
php7.1-gd \
php7.1-soap \
php7.1-dev \
apt-transport-https \
git \
ssh \
curl \
php-pear \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
# Install Composer
RUN cd /usr/src
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install MSSQL extention
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y install msodbcsql mssql-tools g++ unixodbc-dev make
RUN pear config-set php_ini `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` system
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv
RUN echo "extension=sqlsrv.so" >> /etc/php/7.1/fpm/php.ini
RUN echo "extension=pdo_sqlsrv.so" >> /etc/php/7.1/fpm/php.ini
# Fixed locals for MSSQL extention
RUN apt-get install -y locales
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
RUN locale-gen
WORKDIR /var/www
Inside the docker container I can't ping the SQL server. So for me i sounds like a network issue, but I'm unable find a solution.
Please note the SQL server is hosted locally on a server in the office.
UPDATE/Solved for now
After downgrading the dokcer for windows to 18.03.0-CE everything worked as expected.
Docker Bridge networks don't connect to the world outside of Docker at all by default; they only allow containers to talk to each other. The documentation for Docker Bridge Networks does offer some advice for allowing Bridge network traffic to talk to the outside world by making changes on the Docker host:
First enable IP forwarding in the kernel:
$ sysctl net.ipv4.conf.all.forwarding=1
Then change the host firewall to allow the forwarding
$ sudo iptables -P FORWARD ACCEPT
This is a fairly permissive firewall configuration, so you may want to look at keeping that a bit more locked down.
The other way would be to attach the container to two different networks: your current bridge network for communication between container, and a second Host network for talking to MySQL. Since this bypasses all of Docker's NAT configuration, the scalability of your service may be impacted, although I think outgoing connections might be OK.
For my case:
I ran these below:
To see all containers:
docker ps -a
Then restart container:
docker container restart yourIdContainer
If there is error: Error response from daemon: Cannot restart container ...
Please restart Docker then restart container again.
Connect to Sql Server by MSSM, server name: localhost,1433 or IP
Hope this is helpful.
If you are using Windows 10 (HOME VERSION ) may you don't have the virtualization and you are using (docker toolbox ), they have solved the problem using a static ip. Check it.
I mean localhost won't work if you are using docker toolbox, search the ip where is virtualized docker toolbox.
My case ip: 192.168.99.100

Resources