microsoft / mssql-django

The Microsoft Django backend for SQL Server provides a connectivity layer for Django on SQL Server or Azure SQL DB.
Other
351 stars 115 forks source link

Unable to connect from Docker container running django to SQL Server using the FreeTDS driver #393

Closed josuablejeru closed 2 weeks ago

josuablejeru commented 6 months ago

Software versions

Table schema and Model No schema yet, as it is a fresh project.

Database Connection Settings

DATABASES = {
    "default": {
        "ENGINE": "mssql",
        "NAME": "ZollApp",
        "USER": "zoll",
        "PASSWORD": "123passwordZoll",
        "HOST": "zoll_app_mssql",  # Container Name
        "OPTIONS": {
            "driver": "FreeTDS",
            "unicode_results": True,
            "host_is_server": True,
            "driver_supports_utf8": True,
        },
    }
}

Problem description and steps to reproduce When attempting to apply migrations to create the initial schemas, I encounter an error stating: "Adaptive Server is unavailable or does not exist." Although I am able to ping the SQL Server from my "web" container where Django is running, I am unable to establish a connection with the mssql-django library. Interestingly, I can successfully access the database using a SQL client with the same credentials. In the past I used SqlAlchemy with pymsql with the same docker setup and it worked flawlessly.

Error message/stack trace

django.db.utils.OperationalError: ('08S01', '[08S01] [FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)')

Any other details that can be helpful

FROM --platform=linux/amd64/v2 ubuntu:24.04

WORKDIR /opt/

# Set environment variables
ENV PYTHONFAULTHANDLER=1 \
  PYTHONUNBUFFERED=1 \
  PYTHONHASHSEED=random \
  PIP_NO_CACHE_DIR=off \
  PIP_DISABLE_PIP_VERSION_CHECK=on \
  PIP_DEFAULT_TIMEOUT=100 \
  # Poetry's configuration:
  POETRY_NO_INTERACTION=1 \
  POETRY_VIRTUALENVS_CREATE=false \
  POETRY_VIRTUALENVS_IN_PROJECT=false \
  POETRY_CACHE_DIR='/var/cache/pypoetry' \
  POETRY_VERSION=1.7.1

RUN apt-get update && apt-get install -y curl build-essential apt-transport-https gpg
# adding custom MS repository
RUN curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg
RUN curl -fsSL https://packages.microsoft.com/config/ubuntu/24.04/prod.list | tee /etc/apt/sources.list.d/mssql-release.list

# install SQL Server drivers
RUN apt-get update 
RUN ACCEPT_EULA=Y apt-get install -y python3.12 python3.12-dev python3-pip python3.12-venv unixodbc-dev unixodbc freetds-dev freetds-bin tdsodbc libpango-1.0-0 libpangoft2-1.0-0
RUN apt-get install -y iputils-ping

# Install Poetry and Dependencies
RUN pip3 install --break-system-packages --upgrade pip setuptools
ARG POETRY_VERSION=1.8.2
ENV POETRY_HOME=/opt/poetry\
  PATH="${PATH}:/opt/poetry/bin"
RUN python3 -m venv ${POETRY_HOME} &&\
  ${POETRY_HOME}/bin/pip install poetry==${POETRY_VERSION} &&\
  poetry config virtualenvs.create false

# Copy only the dependencies files to the container
COPY poetry.lock pyproject.toml /opt/
# Install project dependencies
RUN poetry install --no-root --no-interaction --no-ansi --no-dev

# Copy the rest of the application code to the container
COPY . /opt/

# Expose the port Gunicorn will listen on
EXPOSE 8000

RUN python3 /opt/zoll_app/manage.py migrate --noinput

# Run gunicorn
CMD ["python3", "/opt/zoll_app/manage.py", "runserver", "0.0.0.0:8000"]
services:
  sqlserver:
    image: mcr.microsoft.com/mssql/server:2019-latest
    container_name: zoll_app_mssql
    restart: always
    environment:
      MSSQL_SA_PASSWORD: "Zoll#1337"
      ACCEPT_EULA: "Y"
      MSSQL_PID: Developer
    ports:
      - 1433:1433
    volumes:
      - ./.docker/mssql-data:/var/opt/mssql

  redis:
    image: redis:latest
    container_name: zoll_app_redis
    restart: always
    command: ["redis-server", "--appendonly", "no"]
    ports:
      - 6379:6379
    volumes:
      - ./.docker/redis/data:/data

  flower:
    image: mher/flower:0.9.7
    container_name: zoll_app_flower
    command: ["flower", "--broker=redis://redis:6379", "--port=5555"]
    ports:
      - 5555:5555
    depends_on:
      - redis

  web:
    build:
      context: .
      dockerfile: Dockerfile
    platform: linux/amd64/v2
    container_name: zoll_app_web
    restart: always
    volumes:
      - .:/zoll_app
    ports:
      - "8000:8000"
    depends_on:
      - sqlserver
      - redis

Thanks for your help 🤗

absci commented 6 months ago

Thanks for providing the details, I haven't tested your whole setup yet. But from the OS version, currently ODBC does not have official support for 24.04 yet. Could you try earlier versions?