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