Skip to content

Instantly share code, notes, and snippets.

@jangaraj
Last active March 22, 2024 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jangaraj/bf9a1bbf71bd6d4b9616828b29096d19 to your computer and use it in GitHub Desktop.
Save jangaraj/bf9a1bbf71bd6d4b9616828b29096d19 to your computer and use it in GitHub Desktop.
Dockerfile to build pyodbc and unixODBC for MSSQL as a lambda layer for Python 3.8 in the Dockerfile
# inspired by https://gist.github.com/diriver63/b72a954fa0da4851d89e5086aa13c6e8
# build from online Dockerfile (use always URL to the latest Dockerfile):
# docker build --rm=true -t local/pyodbc-layer https://gist.githubusercontent.com/jangaraj/bf9a1bbf71bd6d4b9616828b29096d19/raw/a3a5f35dbafd50b9623c9d3886cb3f4b2d800d92/Dockerfile
# build from the local Dockerfile
# docker build --rm=true -t local/pyodbc-layer .
# copy pyodbc-layer.zip from the image and destroy image
# docker run -d --name delme local/pyodbc-layer sleep 1000 && docker cp delme:/tmp/pyodbc-layer.zip . && docker rm -f delme && docker rmi -f local/pyodbc-layer
FROM lambci/lambda:build-python3.8
ENV \
ODBC_VERSION=2.3.7 \
ODBCINI=/opt/odbc.ini \
ODBCSYSINI=/opt/
RUN \
curl ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-${ODBC_VERSION}.tar.gz -O && \
tar xzvf unixODBC-${ODBC_VERSION}.tar.gz && \
cd unixODBC-${ODBC_VERSION} && \
./configure --sysconfdir=/opt --disable-gui --disable-drivers --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --prefix=/opt && \
make && \
make install && \
cp include/*.h /usr/include/ && \
cd .. && \
rm -rf unixODBC-${ODBC_VERSION} unixODBC-${ODBC_VERSION}.tar.gz && \
curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo && \
yum install -y e2fsprogs.x86_64 0:1.43.5-2.43.amzn1 fuse-libs.x86_64 0:2.9.4-1.18.amzn1 libss.x86_64 0:1.43.5-2.43.amzn1 openssl && \
ACCEPT_EULA=Y yum install -y msodbcsql17 --disablerepo=amzn* && \
export CFLAGS="-I/opt/include" && \
export LDFLAGS="-L/opt/lib" && \
cd /opt && \
cp -r /opt/microsoft/msodbcsql17/ . && \
rm -rf /opt/microsoft/ && \
mkdir /opt/python/ && \
cd /opt/python/ && \
pip install pyodbc -t . && \
cd /opt && \
echo $'[ODBC Driver 17 for SQL Server]\nDescription=Microsoft ODBC Driver 17 for SQL Server\n \
Driver=/opt/msodbcsql17/lib64/libmsodbcsql-17.5.so.2.1\nUsageCount=1\n' > odbcinst.ini && \
echo '[ODBC Driver 17 for SQL Server]\nDriver = ODBC Driver 17 for SQL Server\n \
Description = My ODBC Driver 17 for SQL Server\nTrace = No\n' > odbc.ini && \
rm -rf {include,share} && \
zip -r9 /tmp/pyodbc-layer.zip .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment