【问题标题】:How to install pyodbc on Dockerfile如何在 Dockerfile 上安装 pyodbc
【发布时间】:2023-02-17 20:23:22
【问题描述】:

我正在尝试在 Django 上安装 pyodbc 以访问 Sql Server,但尚未构建 Docker 映像。

文件:

FROM --platform=linux/amd64 python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y && apt-get install -y gcc curl gnupg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update -y && apt-get install -y unixodbc unixodbc-dev tdsodbc freetds-common freetds-bin freetds-dev postgresql
RUN apt-get update && ACCEPT_EULA=Y apt-get -y install mssql-tools msodbcsql17
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN apt-get update
RUN mkdir /djangonoguero
COPY ./project /djangonoguero/
COPY ./requirements.txt /djangonoguero/
COPY odbcinst.ini /etc/
COPY odbc.ini /etc/
COPY freetds.conf /etc/freetds/
WORKDIR /djangonoguero
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000

我通过 docker cli 终端得到输出错误

=> ERROR [16/16] RUN pip install --no-cache-dir -r requirements.txt                                                                                                                                                                      155.7s
------
> [16/16] RUN pip install --no-cache-dir -r requirements.txt:
...
#20 93.38 Building wheels for collected packages: pyodbc, dj-static, static3, django-ckeditor, odfpy, anyjson, django-celery, pyftpdlib
#20 93.38   Building wheel for pyodbc (setup.py): started
#20 96.49   Building wheel for pyodbc (setup.py): finished with status 'error'
#20 96.49   ERROR: Command errored out with exit status 1:
#20 96.49    command: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/setup.py'"'"'; __file__='"'"'/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"',     '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-_ty0puya
#20 96.49        cwd: /tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/
#20 96.49   Complete output (13 lines):
#20 96.49   running bdist_wheel
#20 96.49   running build
#20 96.49   running build_ext
#20 96.49   building 'pyodbc' extension
#20 96.49   creating build
#20 96.49   creating build/temp.linux-x86_64-3.8
#20 96.49   creating build/temp.linux-x86_64-3.8/tmp
#20 96.49   creating build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm
#20 96.49   creating build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138
#20 96.49   creating build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/src
#20 96.49   gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DPYODBC_VERSION=3.0.10 -DPYODBC_UNICODE_WIDTH=4 -DSQL_WCHART_CONVERT=1 -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include -I/usr/local/include/python3.8 -c /tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/src/sqlwchar.cpp -o build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/src/    sqlwchar.o -Wno-write-strings
#20 96.49   gcc: error trying to exec 'cc1plus': execvp: No such file or directory
#20 96.49   error: command 'gcc' failed with exit status 1
#20 96.49   ----------------------------------------
#20 96.49   ERROR: Failed building wheel for pyodbc
#20 96.49   Running setup.py clean for pyodbc

...

#20 133.2 Failed to build pyodbc
#20 135.2 Installing collected packages: wcwidth, vine, six, prompt-toolkit, click, amqp, urllib3, sqlparse, pytz, python-dateutil, kombu, jmespath, docutils, click-repl, click-plugins, click-didyoumean, billiard, text-unidecode, Django, celery, botocore, static3, s3transfer, pyodbc, pyftpdlib, Pillow, numpy, idna, Faker, et-xmlfile, django-js-asset, django-celery, django-appconf, defusedxml, chardet, certifi, requests, reportlab, psycopg2-binary, ping3, pandas, openpyxl, odfpy, honcho,     gunicorn, factory-boy, djangorestframework, django-widget-tweaks, django-storages, django-queued-storage, django-mssql-backend, django-ftpserver, django-filter, django-environ, django-cors-headers, django-ckeditor, dj-static, boto3, boto, asgiref, anyjson
#20 151.2     Running setup.py install for pyodbc: started
#20 154.3     Running setup.py install for pyodbc: finished with status 'error'
#20 154.3     ERROR: Command errored out with exit status 1:
#20 154.3      command: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/setup.py'"'"'; __file__='"'"'/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"',     '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-fgdyreqq/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/pyodbc
#20 154.3          cwd: /tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/
#20 154.3     Complete output (13 lines):
#20 154.3     running install
#20 154.3     running build
#20 154.3     running build_ext
#20 154.3     building 'pyodbc' extension
#20 154.3     creating build
#20 154.3     creating build/temp.linux-x86_64-3.8
#20 154.3     creating build/temp.linux-x86_64-3.8/tmp
#20 154.3     creating build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm
#20 154.3     creating build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138
#20 154.3     creating build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/src
#20 154.3     gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DPYODBC_VERSION=3.0.10 -DPYODBC_UNICODE_WIDTH=4 -DSQL_WCHART_CONVERT=1 -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include -I/usr/local/include/python3.8 -c /tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/src/sqlwchar.cpp -o build/temp.linux-x86_64-3.8/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/src/    sqlwchar.o -Wno-write-strings
#20 154.3     gcc: error trying to exec 'cc1plus': execvp: No such file or directory
#20 154.3     error: command 'gcc' failed with exit status 1
#20 154.3     ----------------------------------------
#20 154.3 ERROR: Command errored out with exit status 1: /usr/local/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/setup.py'"'"'; __file__='"'"'/tmp/pip-install-6hbca4vm/pyodbc_01637cbf594442cbbc4aac8a8305e138/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code =     f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-fgdyreqq/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.8/pyodbc Check the logs for full command output.
#20 155.0 WARNING: You are using pip version 21.2.4; however, version 22.0.3 is available.
#20 155.0 You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
------
executor failed running [/bin/sh -c pip install --no-cache-dir -r requirements.txt]: exit code: 1

问题应该出在某些库上,但我不知道是 Dockerfile 还是 python 要求级别,我无法解决。

有人可以帮我吗? 提前致谢。

【问题讨论】:

  • 请附上你的 Dockerfile
  • cc1plus 是 GCC 的内部部分。鉴于这个 Dockerfile,看到这个错误我有点惊讶;安装 build-essential 元数据包而不是仅仅安装 gcc 会有什么不同吗?

标签: sql-server django docker


【解决方案1】:

编译器只是抱怨构建时间依赖性,cc1 工具应该在您的系统中构建 pyodbc。

在 Ubuntu 中你可以用

sudo apt-get update
sudo apt-get install --reinstall build-essential

对于您的 Dockerfile,这似乎工作正常,我在第 5 行添加了 build-essential

FROM --platform=linux/amd64 python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y && apt-get install -y gcc curl gnupg build-essential
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update -y && apt-get install -y unixodbc unixodbc-dev tdsodbc freetds-common freetds-bin freetds-dev postgresql
RUN apt-get update && ACCEPT_EULA=Y apt-get -y install mssql-tools msodbcsql17
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN apt-get update
RUN mkdir /djangonoguero
COPY ./project /djangonoguero/
COPY ./requirements.txt /djangonoguero/
COPY odbcinst.ini /etc/
COPY odbc.ini /etc/
COPY freetds.conf /etc/freetds/
WORKDIR /djangonoguero
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000

PS:如果你觉得不够冒险,可以尝试一个一个地安装包,build-essential包含了很多工具;从这里你可以压缩图像大小。

【讨论】:

  • python:3.8-slim-buster 不是 Alpine 图像,您也不需要在此图像中使用 sudo
  • 我回答的时候没有提到docker file,所以我索取并猜测了一下,给出了一个笼统的答案。
【解决方案2】:

在 Debian 11 上,这对我有用:

RUN apt install curl
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN exit
RUN apt-get update
RUN echo msodbcsql18 msodbcsql/ACCEPT_EULA boolean true | debconf-set-selections
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools18
RUN echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc 
    && /bin/bash -c "source ~/.bashrc"

对于 DRIVER = ODBC 18 SQL 驱动程序

【讨论】:

    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2022-10-16
    • 1970-01-01
    相关资源
    最近更新 更多