0

I'm trying to access a SQL Server database running on another machine on the network, from an Alpine Docker image, running on an Ubuntu machine. So far, I can access the database from the Ubuntu machine, but I can't connect to it from inside the container. By setting network_mode: host inside my docker-compose.yml file, I can ping the SQL Server database, but I still can't connect to it with isql, tsql, or the Python module pyodbc.

I can use tsql to locate the database from the host and the container, but I can't connect to the database from the container.

# works fine from host and container
tsql -H apex-data.apex.local -L

# only works from host
tsql -S apex-data -p 1433 -U condor
# or alternatively with /etc/odbc.ini
isql APEXDSN

Here is my Dockerfile:

FROM python:3.6-alpine3.7

ENV PYTHONUNBUFFERED 1

# update apk repo
RUN echo "http://dl-4.alpinelinux.org/alpine/v3.7/main" >> /etc/apk/repositories && \
    echo "http://dl-4.alpinelinux.org/alpine/v3.7/community" >> /etc/apk/repositories

RUN apk update && apk upgrade && \
    apk add unixodbc unixodbc-dev && \
    apk add freetds && \
    apk add build-base

WORKDIR /usr/src/project

COPY requirements.txt ./
RUN pip install -r requirements.txt

COPY odbc.ini /etc/odbc.ini
COPY odbcinst.ini /etc/odbcinst.ini

COPY project ./

My docker-compose.yml looks like:

version: '3.2'

services:

  app:
    build: .
    container_name: "nf-app"
    command: python3 app.py
    volumes:
      - ./project:/usr/src/project
    ports:
      - "3001:3001"
      - "1433:1433"
    network_mode: host

Here is the odbcinst.ini file:

[FreeTDS]
Description = TD Driver (MSSQL)
Driver = /usr/lib/libtdsodbc.so.0

[ODBC]
Trace = Yes
ForceTrace = Yes
TraceFile = /usr/src/project/logs/odbc.log
cjohnson318
  • 2,922
  • 25
  • 33
  • And what hostname are you using to connect to SQL from inside container? – grapes Dec 04 '18 at 06:52
  • It's a domain name on the network, something like `apex-data.apex.local`. I also tried using the IP address, but that didn't work an better. – cjohnson318 Dec 04 '18 at 15:02
  • As soon as your container is running in `host` network mode, you connect to SQL with `localhost` from inside – grapes Dec 04 '18 at 15:06
  • Even if the IP address of the SQL server is something else, like 192.168.xxx.xxx? – cjohnson318 Dec 04 '18 at 15:09
  • Your SQL runs on same machine as `docker`? If yes, the actual IP does not matter if you are using network mode `host` – grapes Dec 04 '18 at 15:14
  • No, the SQL server runs on another machine on the network. – cjohnson318 Dec 04 '18 at 15:16
  • Ah, sorry, my bad, didnt get it from question – grapes Dec 04 '18 at 15:17
  • Did you check /etc/odbcinst.ini is accessible by the app and/or odbc is working fine ? It seems like the problem is not related with Docker networking. – Sami Altundag Dec 04 '18 at 15:21
  • Btw `host` network mode is incompatible with `port forwarding`. https://docs.docker.com/compose/compose-file/#ports – grapes Dec 04 '18 at 15:23
  • @grapes I saw that `host` mode doesn't allow links, but I didn't know about port forwarding, thanks. – cjohnson318 Dec 04 '18 at 15:27
  • @SamiAltundag how would I check /etc/odbcint.ini to confirm that it's working okay? – cjohnson318 Dec 04 '18 at 15:31
  • You can add these lines to odbcinst.ini file and check logs file in Docker I think. [ODBC] Trace=yes TraceFile=/path/to/file/name.log [ODBC Drivers] – Sami Altundag Dec 04 '18 at 15:34
  • @SamiAltundag okay, I added the logging, but it doesn't provide much more than the error message I saw in the `tsql` output: `[[unixODBC][FreeTDS][SQL Server]Unable to connect to data source]` – cjohnson318 Dec 04 '18 at 16:57
  • If you use Host name, it works. If you use, -S it does not work. So something should be wrong with odbc config. Have you checked https://stackoverflow.com/questions/18232386/freetds-working-but-odbc-cannot-connect ? Sorry I could not help more. – Sami Altundag Dec 04 '18 at 17:03
  • @SamiAltundag that looks very promising! Thank you for the link! – cjohnson318 Dec 04 '18 at 17:11

0 Answers0