django - [FreeTDS][SQL Server]用户登录失败

标签 django linux docker freetds django-pyodbc-azure

我正在 Windows 上进行开发,并尝试使用 Gunicorn 和 Nginx 在 Linux 容器中运行 Django 应用程序,以将其部署到生产环境中的 Linux 机器上。

我主要用这个Connect docker python to SQL server with pyodbc作为指南发布,但我想我已经尝试了在网上找到的关于此错误的所有解决方案。

如果我从容器中 ping DB 服务器,它会连接,因此端口 1433 已打开,一切都应该顺利进行。但由于某种原因,我收到错误 django.db.utils.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Login failed for user

Django settings.py

DATABASES = {
    'default': {
        'ENGINE': "sql_server.pyodbc",
        'NAME': 'database1',
        'HOST': '123.45.6.78',
        'PORT':'1433',
        'USER': "user",
        'PASSWORD': "pswd",
        'OPTIONS': {
            "driver": "FreeTDS",
            "host_is_server": True,
            "unicode_results": True,
            "extra_params": "tds_version=7.3",
        }
    }
}

Docker 文件

# start from an official image
FROM python:3

# arbitrary location choice: you can change the directory
RUN mkdir -p /opt/services/djangoapp/src
WORKDIR /opt/services/djangoapp/src

#Install FreeTDS and dependencies for PyODBC
RUN apt-get update \
 && apt-get install unixodbc -y \
 && apt-get install unixodbc-dev -y \
 && apt-get install freetds-dev -y \
 && apt-get install freetds-bin -y \
 && apt-get install tdsodbc -y \
 && apt-get install --reinstall build-essential -y

# populate "ocbcinst.ini"
RUN echo "[FreeTDS]\n\
TDS_Version = '7.3'\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini

# modify "freetds.conf"
RUN echo "[mssql]\n\
host = 172.30.2.18\n\
port = 1433\n\
tds version = 7.3" >> /etc/freetds/freetds.conf

RUN echo MinProtocol = TLSv1.0 >> /etc/ssl/openssl.cnf
RUN echo CipherString = DEFAULT@SECLEVEL=1 >> /etc/ssl/openssl.cnf


# install our dependencies
# we use --system flag because we don't need an extra virtualenv
COPY Pipfile Pipfile.lock /opt/services/djangoapp/src/
RUN pip install pipenv && pipenv install --system

# copy our project code
COPY . /opt/services/djangoapp/src

# expose the port 8000
EXPOSE 8000

# define the default command to run when starting the container
CMD ["gunicorn", "--chdir", "app", "--bind", ":8000", "config.wsgi:application"]

docker-compose.yml

version: '3'

services:

  djangoapp:
    build: .
    volumes:
      - .:/opt/services/djangoapp/src
      - /static:/static
    networks:  # <-- here
      - nginx_network

  nginx:
    image: nginx:1.13
    ports:
      - 8000:80
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - /static:/static
    depends_on:
      - djangoapp
    networks:  # <-- here
      - nginx_network

networks:  # <-- and here
  nginx_network:
    driver: bridge

Pip文件

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
django = "==2.1.0"
pyodbc = "==4.0.28"
django-pyodbc-azure = "*"
django-datatables-view = "*"            
gunicorn = "*"
whitenoise = "*"


[requires]
python_version = "3.8"

最佳答案

终于自己解决了。必须在 .yml 文件中将端口 1433 添加到 nginx。

像这样

  nginx:
    image: nginx:1.13
    ports:
      - 8000:80
      - 1433:1433
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - /static:/static
    depends_on:
      - djangoapp
    networks:  # <-- here
      - nginx_network

关于django - [FreeTDS][SQL Server]用户登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60396756/

相关文章:

python - 模板中的模板。如何避免渲染两次?

可以延迟分配静态内存吗?

c - gdb 如何检查二维数组元素

postgresql - Docker 容器中 PostgreSQL 的权限问题

docker - Traefik的原木在哪里?

python - 在 db 中保存二进制对象时,django-debug-toolbar 会崩溃。有解决方法吗?

python - Django 1.7 - 为定义添加或更改 related_name 参数

python - 创建 Django REST Framework ViewSet 时卡住了

linux - 在 Linux/Docker 集群上运行命令

docker - 如何将本地卷移动到远程 docker 机器上