Install ODBC driver in Alpine Linux Docker Container
Asked Answered
T

3

25

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?

Thomasthomasa answered 17/8, 2018 at 3:38 Comment(5)
Probably you will have to compile it. Not everything is available to Alpine.Beaumarchais
Thanks, I ended up using a Debian Stretch Linux image so that I can install stuff with apt-getThomasthomasa
Depending on what you're doing, it worth to stick with Alpine. Unless it gives you too much headache.Beaumarchais
@Thomasthomasa I see your question remains unanswered. Did you find a solution? I want to do something very similar, but only for MS SQL Server.Slice
@Slice 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: github.com/mobydq/mobydq/blob/master/scripts/DockerfileThomasthomasa
A
28

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
Angeles answered 20/11, 2018 at 13:25 Comment(4)
Cool will try that! ThanksThomasthomasa
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.Slice
@Slice 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.Angeles
Increasing the number of run steps slows your docker build, please use apk -U add instead :)Kelleekelleher
B
16

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"]
Bandeen answered 19/10, 2020 at 12:24 Comment(3)
+1 for the SQL Server steps. more info here: learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/…Benedicto
This finally worked for me, thanks :)Ranie
@Bandeen 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! learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/…Icebound
E
0

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

Eatables answered 18/2, 2020 at 11:14 Comment(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 betterThomasthomasa

© 2022 - 2024 — McMap. All rights reserved.