我是docker容器的新手,我一直在努力从Django应用程序连接到mssql数据库,并为我的休息API成功启动docker容器。请耐心等待我长篇大论的问题。
这是我的码头工人文件
FROM ubuntu:19.04
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y curl apt-transport-https python3 python3-pip python-dev locales
RUN apt-get -y update && apt-get install -y sudo apt-utils gnupg ca-certificates
RUN apt-get update && apt-get install -y git
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/19.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update --fix-missing && ACCEPT_EULA=Y apt-get install -y msodbcsql17
# optional: for bcp and sqlcmd
RUN ACCEPT_EULA=Y apt-get install mssql-tools
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN /bin/bash -c "source ~/.bashrc"
# optional: for unixODBC development headers
RUN apt-get -y update && apt-get install -y unixodbc-dev
#RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
#RUN locale-gen
RUN git clone https://github.com/mushfiq/djmsc.git o ms_demo
WORKDIR ms_demo
RUN mkdir -p logs
RUN pip3 install -U pip
#RUN apt-get update && apt-get install -y uwsgi-plugin-python
#RUN apt-get update && apt-get install -y uwsgi
ENV PATH="/usr/local/lib/python3.7:/usr/local/lib/python3.7/dist-packages:${PATH}"
RUN pip3 install -r requirements.txt --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --trusted-host pypi.org
EXPOSE 8000
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
我的要求.txt文件如下所示
Django==2.1.8
pyodbc==4.0.26
django-mssql==1.8
django-pyodbc==1.1.3
django-pyodbc-azure==2.1.0.0
djangorestframework==3.9.2
docker==3.7.3
docker-compose==1.24.1
pymssql==2.1.4
sqlparse==0.3.0
SQLAlchemy==1.3.0
sql_server.pyodbc
这是我从 Django 项目中 settings.py 文件中的数据库设置
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'MyTestDB',
'USER': '',
'PASSWORD': '',
'HOST': 'myMSSQLDBHOST', # Or an IP Address that your DB is hosted on
'PORT': '',
'OPTIONS': {
'provider': 'SQLOLEDB',
'driver': 'SQL Server Native Client 11.0',
'isolation_level': 'READ UNCOMMITTED', # prevent SELECT deadlocks
},
}
}
在 docker 镜像构建命令成功后。当我尝试运行映像时,出现以下错误。
> docker run -p 8000:8000 tanzeelbhatti/ms_demo
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f0f533a18c8>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python3.7/dist-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/usr/local/lib/python3.7/dist-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/usr/local/lib/python3.7/dist-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.7/dist-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/usr/local/lib/python3.7/dist-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/usr/local/lib/python3.7/dist-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/usr/local/lib/python3.7/dist-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py", line 101, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/usr/local/lib/python3.7/dist-packages/django/db/models/base.py", line 305, in add_to_class
value.contribute_to_class(cls, name)
File "/usr/local/lib/python3.7/dist-packages/django/db/models/options.py", line 203, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/usr/local/lib/python3.7/dist-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python3.7/dist-packages/django/db/utils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "/usr/local/lib/python3.7/dist-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/local/lib/python3.7/dist-packages/sql_server/pyodbc/base.py", line 7
except ImportError, e:
^
SyntaxError: invalid syntax
我不确定如何从这里开始。我无法理解我哪里做错了。我想构建一个能够连接到服务器上存在的 MSSQL DB 的映像。我正在使用 docker 工具箱在我的本地窗口机器上运行默认的 docker 机器。从我的本地系统,我使用 Windows 身份验证连接到数据库。
如果您仔细查看错误,则没有迹象表明应用程序无法与 MSSQL 或与数据库连接相关的任何内容进行连接。
File "/usr/local/lib/python3.7/dist-packages/sql_server/pyodbc/base.py", line 7
except ImportError, e:
^
SyntaxError: invalid syntax
代码中存在语法错误。
还会建议使用高山基础 Docker 映像或官方 python 映像,因此几乎不会努力维护应用程序的依赖项。
你可以看看这里和这里。
你的 Django 版本太旧了
Django 1.5 支持 Python 2.6.5 及更高版本。
如果你在Linux下,想要检查你正在使用的Python版本,请从命令行运行python -V。
如果你想检查 Django 版本,打开一个 Python 控制台并键入
导入姜戈 姜戈。版本 (2, 0, 0, 'final', 0(
我相信您可能尚未安装软件包django-mssql-backend
。
pip install django-mssql-backend
这应该会有所帮助。