Docker, .NET Core and Redshift Drivers

Just a quick post to share how to include the Redshift ODBC driver in your .NET Core docker container. This is from the CdcTools.CdcToRedshift application in my CDC Tools repo.

A lot of this was sourced from Sandeep Kunkunru's repo docker-centos-redshift repo.

The .NET code uses the System.Data.Odbc.4.5.0-preview2-26406-04 package. You can see an example of making a Redshift query from the RedshiftDao class.

Dockerfile

FROM microsoft/dotnet:2.0-sdk

# redshift driver section start
ADD ./Docker/install-redshift-drivers.sh /tmp/install-redshift-drivers.sh
ADD ./Docker/env.sh /tmp/env.sh

RUN /tmp/install-redshift-drivers.sh

ADD ./Docker/odbc.ini /etc/odbc.ini
ADD ./Docker/odbcinst.ini /etc/odbcinst.ini
ADD ./Docker/amazon.redshiftodbc.ini /opt/amazon/redshiftodbc/lib/64/amazon.redshiftodbc.ini

RUN /tmp/env.sh
# redshift driver section end

ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "CdcTools.CdcToRedshift.dll"]

Scripts

install-redshift-drivers.sh

#!/bin/bash

# Install the Redshift ODBC driver manager
apt-get update \
    && apt-get install -y --no-install-recommends unixodbc

if ! wget https://s3.amazonaws.com/redshift-downloads/drivers/odbc/1.4.1.1001/AmazonRedshiftODBC-64-bit-1.4.1.1001-1.x86_64.deb; then
    echo 'Failed to download Redshift ODBC Driver!' 1>&2
    exit 1
fi

# Install the Redshift ODBC driver
apt install ./AmazonRedshiftODBC-64-bit-1.4.1.1001-1.x86_64.deb

env.sh

#!/bin/bash

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export ODBCINI=/etc/odbc.ini
export AMAZONREDSHIFTODBCINI=/opt/amazon/redshiftodbc/lib/64/amazon.redshiftodbc.ini 
export ODBCSYSINI=/etc

 

ini files

amazon.redshiftodbc.ini

[Driver]
DriverManagerEncoding=UTF-16
ErrorMessagesPath=/opt/amazon/redshiftodbc/ErrorMessages
LogPath=[LogPath]
SwapFilePath=/tmp

#   unixODBC
ODBCInstLib=libodbcinst.so.2

odbc.ini

[ODBC Data Sources]
Amazon_Redshift_x64=Amazon Redshift (x64)

[Amazon Redshift (x64)]
Driver=/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so
Server=blahblah.redshift.amazonaws.com
Port=5439
Database=dev
locale=en-US

odbcinst.ini

[ODBC Drivers]
Amazon Redshift (x64)=Installed

[Amazon Redshift (x64)]
Description=Amazon Redshift ODBC Driver (64-bit)
Driver=/opt/amazon/redshiftodbc/lib/64/libamazonredshiftodbc64.so