在 docker 上连接 python 和 freetds sql server

问题描述 投票:0回答:0

我正在尝试使用 FreeTDS 将我的 python 应用程序连接到 Sql Server,但找不到

libtdsodbc.so

这是我的 Dockerfile:

# Pull base image
FROM python:3.10

WORKDIR /code/

RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini

RUN apt-get update
RUN apt-get install -y --no-install-recommends \
    tdsodbc \
    unixodbc-dev \
    unixodbc \
    libpq-dev
RUN apt-get clean -y

ADD requirements.txt /

ARG PIP_EXTRA_URL=https://<...>/pypi/simple/

RUN pip install --upgrade pip
RUN pip install -r /requirements.txt --extra-index-url $PIP_EXTRA_URL --no-cache-dir

COPY . /code/

EXPOSE 80/tcp
EXPOSE 443/tcp

这是我的 docker-compose.yml:

version: "3.5"

networks:
  datamodel-network:
    name: datamodel-network
services:
  db:
    container_name: 'data-model-db'
    image: "mcr.microsoft.com/mssql/server"
    ports:
      - "1433:1433"
    networks:
      - datamodel-network
    environment:
      SA_PASSWORD: "DEV_mssql-PW_356.4,DO_NOT_use,this.PW,for-prod"
      ACCEPT_EULA: "Y"
  web:
    container_name: 'datamodel'
    build: .
    command: bash -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 80 --reload"
    volumes:
      - .:/code
    ports:
      - "8000:80"
    depends_on:
      - db
    links:
      - db
    networks:
      - datamodel-network
    environment:
      IS_DOCKER: "true"
      LOCAL_CONNECTION_STRING: "mssql+pyodbc:///?odbc_connect=Driver={FreeTDS};SERVER=db,1433;DATABASE=insights_v1;trusted_connection=yes;"

当我运行

docker-compose up
我得到以下异常:

sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]无法打开 lib '/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so ' : 找不到文件 (0) (SQLDriverConnect)")

或者是否有更简单的解决方案来连接它们?

python sql-server docker freetds
© www.soinside.com 2019 - 2024. All rights reserved.