0

I'm trying to dockerize an application that has previously deployed on windows and I was wondering what python image I should use in order to resolve an issue with installing pyodbc and pythonnet. My requirements:

pyodbc==4.0.25 pythonnet==2.4.0

the pyodbc error I'm getting states:

src/pyodbc.h:56:10: fatal error: sql.h: no such file or directory

the pythonnet error: No matching distribution found for pythonnet==2.4.0

My Dockerfile looks like this:

FROM python:3.6
WORKDIR /opt

# create a virtual environment and add it to PATH so that it is applied for all subsequent run and cmd calls
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY requirements.txt 
COPY src ./src

RUN pip install --no-cache-dir -r requirements.txt
cmd python src/automation/interface/app.py

I tried pulling a windows-server-core image but got an error message saying the image could not be found, and I haven't been able to find any other functioning windows-based image. I need to resolve this because my application builds and imports a c# app, and I need pythonnet in order to do that. Any help would be much appreciated.

Boris
  • 716
  • 1
  • 4
  • 25

1 Answers1

0

Here is a Dockerfile you can use

FROM python:3.6
WORKDIR /opt

# Install Mono for pythonnet.
RUN apt-get update \
    && apt-get install --yes \
        apt-transport-https \
        dirmngr \
        clang \
        gnupg \
        ca-certificates \
        # Dependency for pyodbc.
        unixodbc-dev \
    && apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \
    && echo "deb http://download.mono-project.com/repo/debian stretch/snapshots/5.20 main" | tee /etc/apt/sources.list.d/mono-official-stable.list \
    && apt-get update \
    && apt-get install --yes \
        # https://github.com/pythonnet/pythonnet/issues/939#issuecomment-520904067
        # https://github.com/jonemo/pythonnet-docker/blob/master/_dockerfiles/python3.6.10-mono5.20-pythonnet2.5.0
        mono-devel=5.20\* \
    && rm -rf /var/lib/apt/lists/*

# create a virtual environment and add it to PATH so that it is applied for all subsequent run and cmd calls
ENV VIRTUAL_ENV=/opt/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN python3 -m venv $VIRTUAL_ENV \
    # From here on, use virtual env's python.
    && python -m pip install --no-cache-dir --upgrade pip setuptools wheel \
    # Dependency for pythonnet.
    && python -m pip install --no-cache-dir pycparser \
    && python -m pip install --no-cache-dir "pyodbc==4.0.25" "pythonnet==2.4.0"

This Dockerfile installs the Mono runtime, which seems to be necessary for pythonnet on linux. It also solves the missing sql.h file by installing unixodbc-dev (see mkleehammer/pyodbc#441).

jkr
  • 17,119
  • 2
  • 42
  • 68
  • I'm trying to install pyodbc not pyobjc. I managed to install it after downloading some additional drivers. As for the 2nd part of your answer, I'm not sure if I can use this; I'm trying to pip install python net, not deploy an additional container. – Boris Mar 03 '21 at 14:39
  • thanks, but why are the github urls commented out? – Boris Mar 03 '21 at 16:04
  • @Boris the first commented out url is really just a comment explaining why you might have to use Mono 5.20. I think we fixed the corresponding issue with 6.0 in pythonnet 2.5 – LOST Mar 03 '21 at 18:11
  • yes i included those links as comments to point to discussions regarding pythonnet and mono version 5.20.x. @LOST - please feel free to submit a different answer that modifies the dockerfile i included – jkr Mar 03 '21 at 19:09
  • @jakub I'm still getting an error saying "failed building wheel for pythonnet". Did you test this code yourself? – Boris Mar 11 '21 at 13:13
  • Yes I tried this. And I was able to build the image right now. Are you using exactly the Dockerfile I posted? – jkr Mar 11 '21 at 16:56