Unable to locate package msodbcsql on Python image

Question:

This is the first time that I’m trying to use pyodbc to connect to an Azure SQL Database within a docker image. My Dockerfile looks like the below:

# the base image
FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./
COPY music_trends.py ./

# install SQL Server drivers
RUN apt-get update
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql unixodbc-dev

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./music_trends.py" ]

Which throws the error message:

E: Unable to locate package msodbcsql
The command '/bin/sh -c apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql unixodbc-dev' returned a non-zero code: 100

I have found resolutions for ubuntu:16.04 such as: https://github.com/Azure/azure-functions-docker/pull/45 and have also tried to run the msodbcsql.msi files from my Dockerfile.

Is there an equivalent fix for python:3?

Asked By: James

||

Answers:

python:3 is based on debian, so refer to microsoft doc:

You should install microsoft apt source, meanwhile change msodbcsql to msodbcsql17, example as next:

Dockerfile:

FROM python:3

RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && 
    curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list && 
    apt-get update && 
    ACCEPT_EULA=Y apt-get install msodbcsql17 unixodbc-dev -y

UPDATE 2019-07-26:

I didn’t notice official python:3 image update from debian 9 to debian 10 early this month, see this

From microsoft guide above, it seems currently they just package every dependency ok for next:

#Debian 8
curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Debian 9
curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list

Of course you can handle debian 10 dependency by yourself, such as libcrypto.so version issue etc, but I still suggest you just use python3 debian 9 version as microsoft did everything for you (PS: I think they will update in near future just because debian 10 release half month ago, I guess they need some time. BTW, https://packages.microsoft.com/config/debian/10/prod.list is there, but it does not have the package msodbcsql17 currently…)

So what I suggest easiest way for you is next, compared to the old Dockerfile, just change python:3 to python:3-stretch, and also install apt-transport-https which default not installed in debian 9, detail as follows:

Dockerfile:

FROM python:3-stretch

RUN apt-get update && 
    apt-get install -y apt-transport-https && 
    curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && 
    curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list && 
    apt-get update && 
    ACCEPT_EULA=Y apt-get install msodbcsql17 unixodbc-dev -y

.so check:

root@91addb538736:/# ldd /opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.3.so.1.1
        linux-vdso.so.1 (0x00007ffd72bd0000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4892696000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f489248e000)
        libodbcinst.so.2 => /usr/lib/x86_64-linux-gnu/libodbcinst.so.2 (0x00007f4892273000)
        libcrypto.so.1.0.2 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.2 (0x00007f4891e0d000)
        libkrb5.so.3 => /usr/lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007f4891b33000)
        libgssapi_krb5.so.2 => /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007f48918e8000)
        libssl.so.1.0.2 => /usr/lib/x86_64-linux-gnu/libssl.so.1.0.2 (0x00007f489167f000)
        libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007f489147a000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f48910f8000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4890df4000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4890bdd000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f48909c0000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4890621000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f4892ca1000)
        libk5crypto.so.3 => /usr/lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007f48903ee000)
        libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007f48901ea000)
        libkrb5support.so.0 => /usr/lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007f488ffde000)
        libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007f488fdda000)
        libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f488fbc3000)
Answered By: atline