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
Related
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!
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.
I have generated a docker file for asp.net core API with a single page application thanks to Visual studio. After some research on the web I correct differents trouble about SPA in this docker file.
Finnaly my trouble is the connexion with our database server.
When I tried to connect, I've got a
Microsoft.Data.SqlClient.SqlException : A network-related or instance-specific error occurred while establishing a connection to SQL Server.
It seems that it appears because my container could not acces to the server, after hour and hour of google search, I only found solution with a SQL hosted in docker image.
How to all my docker image of wab app accessing the entire company network to access different server ? I use computer name ant not IP to match company requirement.
Thanks for all
Versions :
.net core api : 3.1
I'm using docker for Windows
docker use linux container
Here is my docker file
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
RUN apt-get update -yq \
&& apt-get install curl gnupg -yq \
&& curl -sL https://deb.nodesource.com/setup_10.x | bash \
&& apt-get install nodejs -yq
WORKDIR /src
COPY ["Company.Dtm.WebApi.AppWebApi/Company.Dtm.WebApi.AppWebApi.csproj", "Company.Dtm.WebApi.AppWebApi/"]
COPY ["CompanyFramework/Company.Framework.WebApi/Company.Framework.WebApi.csproj", "CompanyFramework/Company.Framework.WebApi/"]
COPY ["CompanyFramework/Company.Framework.Model/Company.Framework.Model.csproj", "CompanyFramework/Company.Framework.Model/"]
COPY ["CompanyFramework/Company.Framework.Tools/Company.Framework.Tools.csproj", "CompanyFramework/Company.Framework.Tools/"]
COPY ["AppLib/Company.Dtm.Lib.AppLib/Company.Dtm.Lib.AppLib.csproj", "AppLib/Company.Dtm.Lib.AppLib/"]
RUN dotnet restore "Company.Dtm.WebApi.AppWebApi/Company.Dtm.WebApi.AppWebApi.csproj"
COPY . .
WORKDIR "/src/Company.Dtm.WebApi.AppWebApi"
RUN dotnet build "Company.Dtm.WebApi.AppWebApi.csproj" -c Debug -o /app/build
FROM build AS publish
RUN dotnet publish "Company.Dtm.WebApi.AppWebApi.csproj" -c Debug -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Company.Dtm.WebApi.AppWebApi.dll"]
Here is my docker-compose
version: '3'
services:
webapp:
build: .
network_mode: "bridge"
ports:
- "8880:80"
I also had this issue, just trying to connect to my localhost development SQL Server.
What ended up working was to add the normal SQL Server ports to my Dockerfile:
EXPOSE 1433
EXPOSE 5000 [or whatever other ports you may be using.]
Then set up a firewall Inbound Rule to allow those ports.
You cannot use 'localhost' obviously since the 'localhost' is the host the container it is running in, but I did find with Windows at least, that I can simply use my dev machine name as the server, so it seems that DNS works across the nat. I would think you should be able to access any network resource at that point, but I would say your firewall[s] might be a place to start. Your Docker container acts like an external network and therefore generally un-trusted.
I also found that I did not have a 'bridge' network. Maybe you get that with the Linux container.
My >docker network ls command revealed a "Default Switch" network, but no "bridge". Because this is Docker for Windows, there is no 'host' option.
That was all there was to it for me. I see a lot of other posts talking about a lot of other things, but honestly, just opening up the firewall is what did the trick. Good luck!
You need to add another service for your db in your compose file.
Something like this:
version: "3"
services:
web:
build: .
ports:
- "8000:80"
depends_on:
- db
db:
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "Your_password123"
ACCEPT_EULA: "Y"
make sure to replace the password in the SA_PASSWORD environment variable under db.
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.
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.