简体   繁体   中英

Can't connect to SQL Server from elixir/phoenix in Docker

I'm trying to connect a "Phoenix (Elixir)-Application" (packed as Docker-Container) to microsoft/mssql-server-linux:latest Docker-Container

Therefore I'm using https://hex.pm/packages/mssql_ecto as Adapter

use Mix.Config

config :my_api, MyApi.Repo,
adapter: MssqlEcto,
database: "dev",
username: "sa",
password: "my_api_password",
hostname: "127.0.0.1",
#odbc_driver: "{SQL Server Native Client 11.0}",
#odbc_driver: "{ODBC Driver 11 for SQL Server}",
#odbc_driver: "{ODBC Driver 13.1 for SQL Server}",
#odbc_driver: "{ODBC Driver 13 for SQL Server}",    # <- Default
#port: "1433" # <- Default

My current Dockerfile for running the phoenix-Application in Docker:

FROM elixir:1.5.2

# install ODBC driver
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y curl apt-transport-https debconf-utils apt-utils
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update -y && ACCEPT_EULA=Y apt-get install -y msodbcsql && ACCEPT_EULA=Y apt-get install -y mssql-tools && apt-get install -y unixodbc-dev
RUN apt-get install -y erlang-odbc

I guess the problem is located in my Dockerfile, especially the lines for installing the ODBC driver. When you look at my Mix.Config, you see that i tried many combinations of "odbc-driver"-strings - not one of them works..

heres the output of the phoenix start:

** (Mix) The database for MyApi.Repo couldn't be created: %Mssqlex.Error{constraint_violations: [], message: "[unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql/lib64/libmsodbcsql-11.0.so.2270.0' : file not found Connection to database failed. | ODBC_CODE 01000 | SQL_SERVER_CODE 0", odbc_code: "01000"}

Is anybody out there whos got a working Elixir (docker-image) with the possibility to connect to "microsoft/mssql-server-linux:latest Docker-Container"?

Im very thankful for any help!

BTW ... im able to connect to the "mssql-server-linux Container" from Host via HeidiSQL and Microsoft SQL Server Management Studio 17 => so no problems with "mssql-server-linux Container" at all.

Setup:

  • HOST OS: Windows 10 Pro
  • Docker for Windows: 17.12.0-ce-win47

For all struggling with this: here are the relevant lines of my working Dockerfile for Ubuntu 16:

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install apt-utils apt-transport-https wget
RUN echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/mssql-ubuntu-xenial-release/ xenial main" > /etc/apt/sources.list.d/mssqlpreview.list
RUN apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
RUN apt-get -y update
RUN env ACCEPT_EULA=Y DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
    build-essential \
    cpanminus \
    msodbcsql \
    unixodbc-dev-utf16
RUN apt-get install unixodbc-dev-utf16
RUN cd /root && \
    wget https://cpan.metacpan.org/authors/id/M/MJ/MJEVANS/DBD-ODBC-1.56.tar.gz && \
    tar xf DBD-ODBC-1.56.tar.gz && \
    cd /root/DBD-ODBC-1.56      && \
    perl Makefile.PL -u         && \
    cpanm --installdeps .       && \
    make                        && \
    make test                   && \
    make install                && \
    rm -rf /root/DBD-ODBC-1.56.tar.gz /root/DBD-ODBC-1.56

# Set locale
RUN apt-get install -y locales
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

The last lines are important for the ODBC-Driver to work properly.

odbc_driver: "{ODBC Driver 13 for SQL Server}

for a working MssqlEcto-Configuration!

@Nolan: i was very happy when i get it working with Ubuntu, i switched instantly to Debian (elixir 1.6) - it works. but u have to change some parts. Heres my working Dockerfile for elixir.1.6 (debian) - let me if it works with 1.6.1

BTW: the odbc driver is very bitchy when is comes to wrong locale configuration ;)

FROM elixir:1.6
ENV REFRESHED_AT 29-01-2018


# install odbc driver
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install apt-utils apt-transport-https wget
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/8/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN env ACCEPT_EULA=Y DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
     build-essential \
     cpanminus \
     msodbcsql \
     unixodbc-dev
RUN apt-get install unixodbc-dev
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools
RUN cd /root && \
    wget https://cpan.metacpan.org/authors/id/M/MJ/MJEVANS/DBD-ODBC-1.56.tar.gz && \
    tar xf DBD-ODBC-1.56.tar.gz && \
    cd /root/DBD-ODBC-1.56      && \
    perl Makefile.PL -u         && \
    cpanm --installdeps .       && \
    make                        && \
    make test                   && \
    make install                && \
    rm -rf /root/DBD-ODBC-1.56.tar.gz /root/DBD-ODBC-1.56

# set locale configuration
RUN apt-get install -y locales
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales && \
    update-locale LANG=en_US.UTF-8
ENV LANG en_US.UTF-8

# install hex
RUN mix local.hex --force

# install rebar
RUN mix local.rebar --force

# install phoenix
RUN mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez --force

# install node
RUN curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
RUN bash nodesource_setup.sh
RUN apt-get install nodejs

IDK if this is production ready (in sense of security and optimizing) - when u get an optimized version please let me know! cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM