I wanted to convert some of my Python scripts to Docker containers. Inside of my Python script, I am using pyodbc to make SQL Server selects and inserts. I found an example of how to do this online and it is not functioning how I would expect.
My dockerfile:
FROM python:3.9
RUN apt-get -y update && apt-get install -y curl gnupg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# download appropriate package for the OS version
# Debian 11
RUN curl https://packages.microsoft.com/config/debian/11/prod.list \
> /etc/apt/sources.list.d/mssql-release.list
RUN exit
RUN apt-get -y update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql18
# upgrade pip and install requirements.
COPY /requirements.txt /requirements.txt
RUN pip install --upgrade pip
RUN pip install -r /requirements.txt
COPY . /app
WORKDIR /app
CMD [ "python", "myapp.py" ]
myapp.py
cnxn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
The error I get is this:
cnxn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
pyodbc.OperationalError: ('08001', '[08001] [unixODBC][Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)')
Any help on this would be greatly appreciated.
Related
These commands are pulled from this site: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16#ubuntu18
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev
These are Microsoft's official steps to install ODBC drivers on Ubuntu. I am not too familiar with Linux, so I do not know if using the deprecated apt-key commands will cause me any issues in the future. Could someone let me know if this is a reasonable concern? And if it is, what is the best practice to make my environment more stable for the future?
Thanks
Using azure ubuntu latest agent. how can I install sql server on ubuntu-latest agent in azure pipelines? tried the following but causing errors
- task: Bash#3
inputs:
targetType: 'inline'
script: |
sudo apt-get update
sudo apt-get install mssql-server
You need to Import the public repository GPG keys and register the SQL Server Ubuntu repository.
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add –
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-preview.list)
"
Please refer to the document: Quickstart: Install SQL Server and create a database on Ubuntu
Could you please help me resolve this issue:
I must to say that everything is working fine on Windows amd64 architecture
But when I tried to start docker-compose on arm64 M1 it went wrong and makes the error
So I have these 2 Dockerfiles
FROM --platform=linux/amd64 python:3.9-buster as builder
WORKDIR ...
COPY ...
RUN apt-get update && apt-get install -y git g++ libgirepository1.0-dev unixodbc unixodbc-dev pkg-config libcairo2-dev gcc python3-dev
RUN pip install virtualenv &&\
python -m venv antenv
RUN . antenv/bin/activate &&\
python -m ensurepip --upgrade &&\
pip install --upgrade pip &&\
pip install wheel gobject PyGObject &&\
pip install -r requirements.txt
FROM --platform=linux/amd64 python:3.9-slim
WORKDIR ...
COPY --from=builder ...
RUN apt-get update && apt-get install curl gnupg -y
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - &&\
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list &&\
apt-get update &&\
ACCEPT_EULA=Y apt-get install msodbcsql17 mssql-tools -y &&\
apt-get install -y unixodbc python3-gi gir1.2-secret-1 alembic &&\
. antenv/bin/activate && pip install gunicorn &&\
mkdir -p /opt/antenv/nltk_data
EXPOSE 5000
CMD . antenv/bin/activate && alembic -x data=true upgrade head && gunicorn wsgi:app -w 1 --worker-class eventlet --threads 2 --forwarded-allow-ips=* -b 0.0.0.0:5000
and DataBase:
FROM --platform=linux/amd64 mcr.microsoft.com/mssql/server:2017-latest
WORKDIR /opt/mssql/
# Bundle config source
COPY ./*.sh /opt/mssql/bin/
RUN chmod +x /opt/mssql/bin/*.sh
ENTRYPOINT ["./bin/entrypoint.sh"]
CMD ["tail -f /dev/null"]
As you can see I mentioned everywhere that it is linux/amd64 platform
DataBase is upping good and I don`t get eerors
But when I up Backend, It as if does not see database and I get timeout error
backend | sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
backend | (Background on this error at: https://sqlalche.me/e/14/e3q8)
After the pulling the SQL Server's container, when I want to run it, Docker shows me "exited" status when O execute this command:
docker ps -a
I tried this link but it did not help me:
https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-configure-docker?view=sql-server-ver15#troubleshooting
These are my container logs:
https://paste.fedoraproject.org/paste/dBBFlyulZ1Bera2caa84tA
I using Fedora 29 x86_x64 and my Docker client version is 18.09.6 Build 481bc77 and Docker-Ce is 19.03.4
(Sorry for my bad English)
This is because starting SQL-Server produces an error.
The error can be looked up in the error log.
Unfortunately, you can't access it, becaus the container terminates.
To lookup the error log, you need to have shared that directory...
On the host, create the location where the container stores the data:
sudo mkdir -p /var/opt/mssql
Create the docker-container with the data directory mapped:
docker pull mcr.microsoft.com/mssql/server:2017-latest
docker run -d -p 2017:1433 --name mssql_2017 -e MSSQL_SA_PASSWORD=TOP_SECRET -e ACCEPT_EULA=Y -e MSSQL_PID="Developer" -v /var/opt/mssql:/var/opt/mssql -d mcr.microsoft.com/mssql/server:2017-latest
(note: this maps port 1433 int he container to port 2017 on the host)
then, to start/stop the container:
docker start mssql_2017
docker stop mssql_2017
Now you can now look at the errors in /var/opt/mssql/log on your host machine.
Also, you can check if it works by connecting with sqlcmd:
For Fedora:
sudo su
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo
exit
sudo yum clean expire-cache
sudo yum check-update
sudo yum remove mssql-tools unixODBC-utf16-devel
sudo yum install mssql-tools unixODBC-devel
sudo ln -sfn /opt/mssql-tools/bin/sqlcmd /usr/bin/sqlcmd
sqlcmd -S localhost,2017 -U SA -P "<YourPassword>"
For Ubuntu:
sudo wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)"
sudo apt-get update
sudo apt-get install mssql-tools
sudo ln -sfn /opt/mssql-tools/bin/sqlcmd /usr/bin/sqlcmd
sqlcmd -S localhost,2017 -U SA -P "<YourPassword>"
I am trying to create a docker image that has the following:
php7
apache2
git
xdebug
In my dockerfile I have the official php7 with apache image as the base.
I can run apt-get update.
I have installed git successfully.
But when I run apt-get install -y php-xdebug I get the following:
E: Unable to locate package php-debug
Dockerfile:
FROM php:7.0-apache
RUN apt-get update && apt-get install -y git
RUN apt-get install -y php-xdebug