21

I currently have the following Dockerfile to create my Docker image.

FROM python:3.6.6-alpine3.8

# Add dependencies for Python packages pandas, numpy and pyodbc
RUN apk add --no-cache curl gcc g++ unixodbc-dev
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h

# Project files
ARG PROJECT_DIR=/srv/scripts
RUN mkdir -p $PROJECT_DIR
WORKDIR $PROJECT_DIR
COPY requirements.txt ./

# Install Python dependencies
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

I would like to include various ODBC drivers in this image so that I can use them to connect to different databases from the Python program running in my container.

  • The Python program is using Pyodbc to connect to databases.
  • The ODBC drivers I need to install are:
    • PostgreSQL
    • MySQL
    • Ms SQL Server
    • Teradata
    • Oracle
    • Hive
    • Impala

I wanted to start with PostgreSQL thinking it would be the easiest one but I could not find any package on the Alpine Linux Package manager. Do you have any idea how I should install such a driver?

Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77
  • Probably you will have to compile it. Not everything is available to Alpine. – Paulo Pedroso Aug 20 '18 at 16:14
  • Thanks, I ended up using a Debian Stretch Linux image so that I can install stuff with apt-get – Alexis.Rolland Aug 21 '18 at 00:25
  • Depending on what you're doing, it worth to stick with Alpine. Unless it gives you too much headache. – Paulo Pedroso Aug 21 '18 at 16:53
  • 1
    @Alexis.Rolland I see your question remains unanswered. Did you find a solution? I want to do something very similar, but only for MS SQL Server. – Snympi Nov 20 '18 at 07:46
  • 1
    @Snympi I have used the following base image instead of Alpine « python:3.6.6-slim-stretch » and then I used apt-get ton install drivers. For MsSQL in particular I used FreeTDS. Here is my Dockerfile: https://github.com/mobydq/mobydq/blob/master/scripts/Dockerfile – Alexis.Rolland Nov 21 '18 at 15:16

3 Answers3

25

I was facing the same issue. I solved this issue by adding RUN apk update before RUN apk add commands.(I was using python:3.6-alpine)

Dockerfile

FROM python:3.6-alpine
RUN apk update
RUN apk add gcc libc-dev g++ libffi-dev libxml2 unixodbc-dev mariadb-dev postgresql-dev
Shubham Patel
  • 3,041
  • 24
  • 33
  • Cool will try that! Thanks – Alexis.Rolland Nov 21 '18 at 15:18
  • 2
    Can you please share your entire Dockerfile? Were you also using pyodc in Python to access the databases? If so, was there any further configuration? I'm also keen to get everything going in an Alpine container, but had to resort to Debian for now to get it all going. Want to still get Alpine to work. – Snympi Nov 22 '18 at 06:05
  • 2
    @Snympi I was using pyodbc to connect to databases.I had large number of dependencies(requirements.txt). So I had to move back to ubuntu based image. I will update the answer when I have it working. – Shubham Patel Nov 22 '18 at 06:27
  • Increasing the number of run steps slows your docker build, please use `apk -U add` instead :) – JohannesB May 18 '20 at 16:36
12

As the OP ended moving away from Alpine- to a Debian-base image, and another answer has a small snapshot of a working Dockerfile, I will flesh out a full Dockerfile that builds SQL Server ODBC Driver 17 into a Debian-base image.

# load python 3.8 dependencies using slim debian 10 image.
FROM python:3.8-slim-buster

# build variables.
ENV DEBIAN_FRONTEND noninteractive

# install Microsoft SQL Server requirements.
ENV ACCEPT_EULA=Y
RUN apt-get update -y && apt-get update \
  && apt-get install -y --no-install-recommends curl gcc g++ gnupg unixodbc-dev

# Add SQL Server ODBC Driver 17 for Ubuntu 18.04
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
  && curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list \
  && apt-get update \
  && apt-get install -y --no-install-recommends --allow-unauthenticated msodbcsql17 mssql-tools \
  && echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile \
  && echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc

# upgrade pip and install requirements.
COPY /requirements.txt /requirements.txt
RUN pip install --upgrade pip
RUN pip install -r /requirements.txt

# clean the install.
RUN apt-get -y clean

# copy all files to /app directory and move into directory.
COPY . /app
WORKDIR /app

ENTRYPOINT ["some", "python", "command"]
m.b
  • 311
  • 3
  • 8
  • 3
    +1 for the SQL Server steps. more info here: https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 – Andrew Feb 19 '21 at 15:56
  • 2
    This finally worked for me, thanks :) – nlhnt Dec 27 '21 at 11:40
  • 1
    @m.b your answer has the missing part of the MS document. They did not mention the export path as it should be for Debian. Your code worked for me as well, thank you! https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16#17 – ihsany Jun 07 '22 at 21:27
0

if keeping it lightweight is your concerns, specifically in case of odbc, i recommend using debian:stretch image.

Abhijeet Padwal
  • 143
  • 1
  • 6
  • 1
    Actually I need python so now I am using python:3.6.6-slim-stretch, it’s around 3Mb more than debian:stretch so I think I can hardly do better – Alexis.Rolland Feb 18 '20 at 14:30