docker - 使用 pyodbc 将 docker python 连接到 SQL 服务器

标签 docker dockerfile pyodbc

我正在尝试连接在 docker 容器中运行的 pyodbc python 脚本以登录到 MSSQL 数据库我尝试了各种 docker 文件,但无法建立连接(在构建 docker 或 python 时失败尝试连接),有没有人有一个工作 dockerfile,使用 pyodbc:

Dockerfile:

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Run app.py when the container launches
CMD ["python", "App.py"]

requirements.TXT

pyodbc

App.Py

import pyodbc

connection = pyodbc.connect('Driver={SQL Server};'
                            'Server=xxxx;'
                            'Database=xxx;'
                            'UID=xxxx;'
                            'PWD=xxxx')

cursor = connection.cursor()

cursor.execute("SELECT [Id],[Name] FROM [DCMM].[config].[Models]")
for row in cursor.fetchall():
    print(row.Name)


connection.close()

构建容器 docker build -t sqltest .

输出:

Sending build context to Docker daemon  4.096kB
Step 1/5 : FROM python:2.7-slim
 ---> 426d65ab9a72
Step 2/5 : WORKDIR /app
 ---> Using cache
 ---> 725f35122880
Step 3/5 : ADD . /app
 ---> 3feb8b7744f7
Removing intermediate container 4214091a111a
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in 27aa4dcfe738
Collecting pyodbc (from -r requirements.txt (line 1))
  Downloading pyodbc-4.0.17.tar.gz (196kB)
Building wheels for collected packages: pyodbc
  Running setup.py bdist_wheel for pyodbc: started
  Running setup.py bdist_wheel for pyodbc: finished with status 'error'
  Failed building wheel for pyodbc
  Complete output from command /usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-EfWsmy/pyodbc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpa3S13tpip-wheel- --python-tag cp27:
  running bdist_wheel
  running build
  running build_ext
  building 'pyodbc' extension
  creating build
  creating build/temp.linux-x86_64-2.7
  creating build/temp.linux-x86_64-2.7/src
  gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPYODBC_VERSION=4.0.17 -DSQL_WCHART_CONVERT=1 -I/usr/local/include/python2.7 -c src/cursor.cpp -o build/temp.linux-x86_64-2.7/src/cursor.o -Wno-write-strings
  unable to execute 'gcc': No such file or directory
  error: command 'gcc' failed with exit status 1

  ----------------------------------------
  Running setup.py clean for pyodbc
Failed to build pyodbc
Installing collected packages: pyodbc
  Running setup.py install for pyodbc: started
    Running setup.py install for pyodbc: finished with status 'error'
    Complete output from command /usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-EfWsmy/pyodbc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-BV4sRM-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_ext
    building 'pyodbc' extension
    creating build
    creating build/temp.linux-x86_64-2.7
    creating build/temp.linux-x86_64-2.7/src
    gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPYODBC_VERSION=4.0.17 -DSQL_WCHART_CONVERT=1 -I/usr/local/include/python2.7 -c src/cursor.cpp -o build/temp.linux-x86_64-2.7/src/cursor.o -Wno-write-strings
    unable to execute 'gcc': No such file or directory
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-EfWsmy/pyodbc/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-BV4sRM-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-EfWsmy/pyodbc/
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

最佳答案

需要运行:

sudo apt-get install gcc

需要添加一个odbcinst.ini文件,其中包含:

[FreeTDS]Description=FreeTDS Driver Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so

需要在 docker 文件中添加以下内容

ADD odbcinst.ini /etc/odbcinst.ini
RUN apt-get update
RUN apt-get install -y tdsodbc unixodbc-dev
RUN apt install unixodbc-bin -y
RUN apt-get clean -y

需要将.py中的连接改为

connection = pyodbc.connect('Driver={FreeTDS};'
                            'Server=xxxxx;'
                            'Database=DCMM;'
                            'UID=xxxxx;'
                            'PWD=xxxxx')

现在容器编译,并从 SQL 服务器获取数据

关于docker - 使用 pyodbc 将 docker python 连接到 SQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46405777/

相关文章:

java - Docker alpine + oracle java : cannot find java

sql-server - MSSQL2014 & python 3.7.3 : Trying to get stored procedure return value results in "No results. Previous SQL was not a query."

python - 通过python解析Json数据并更新sql表

Docker ONBUILD COPY 似乎没有复制文件

linux - Docker 镜像创建(weblogic 和 java)

docker - docker 镜像的版本增量

bash - Dockerfile的COPY命令无法正常工作

使用 pyodbc 的 python 类。多行错误

ubuntu - 我需要在 Ubuntu 上使用 Virtual Box 来创建 docker 机器吗?

docker - 使用Letsencrypt的本地Nginx反向代理到Docker容器