Reputation: 61
I am new to docker containers and I have been struggling to connect to mssql database from Django app and successfully launch docker container for my rest API. Please bear with me for long question.
Here is my docker file
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"]
My requirements.txt file look like this
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
And here is my DB setting from settings.py file from Django project
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
},
}
}
After the docker image build command is successful. When I try to run the image I get the following error.
> 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
I am not sure how to proceed from here. I am unable to understand where I am doing wrong. I want to build an image which is capable of connecting to MSSQL DB which exist on the server. I am running default docker machine on my local windows machine using docker toolbox. From my local system i connect to Database using windows authentication.
Upvotes: 0
Views: 1358
Reputation: 66
I believe you may have not installed the package django-mssql-backend
.
pip install django-mssql-backend
This should help.
Upvotes: 0
Reputation: 23
your Django version is too old
Django 1.5 supports Python 2.6.5 and later.
If you're under Linux and want to check the Python version you're using, run python -V from the command line.
If you want to check the Django version, open a Python console and type
import django django.VERSION (2, 0, 0, 'final', 0)
Upvotes: 0
Reputation: 60074
If you look close to your error, there is no indication that the application failed to connect with MSSQL or anything that is related to the DB connection.
File "/usr/local/lib/python3.7/dist-packages/sql_server/pyodbc/base.py", line 7
except ImportError, e:
^
SyntaxError: invalid syntax
There is syntax Error in your code.
Also will suggest using alpine base Docker image or offical python image so will little effort to maintain dependency for your app.
Upvotes: 0