Skip to content

Instantly share code, notes, and snippets.

@comalex
Last active August 17, 2017 22:13
Show Gist options
  • Save comalex/b04b9b1d5c16e2a466ad40fca76db62a to your computer and use it in GitHub Desktop.
Save comalex/b04b9b1d5c16e2a466ad40fca76db62a to your computer and use it in GitHub Desktop.
Set up MS SQL 2005 on MAC with virtual box and docker
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
- "1433:1433"
expose:
- "8000"
FROM lbosqmsft/mssql-python-pyodbc
RUN apt-get install -y freetds-dev
RUN apt-get install -y tdsodbc
EXPOSE 80
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

[IMPORTANT] In most cases you want to use official docker image an article

First install on virtual box Windows Xp and MS SQL server 2005

How to connect ms sql server from mac https://stackoverflow.com/a/37616522/3411682

RESORTE BAC file

If you get error while restoring bak file try to use TSQL

restore filelistonly from disk='C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\INTCON20170815.bak'; to check files which need to move

RESTORE DATABASE INTCON FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\INTCON20170815.bak' with move 'verINTCON' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\INTCON.mdf', move 'verINTCON_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\INTCON_log.ldf'

Use mssql with django < 1.7.x

[IMPORTANT] If you use django > 1.7.x try to use django-pymssql

I was not able to run django_pyodbc on my mac so I used docker image.

*Dockerfile*
FROM lbosqmsft/mssql-python-pyodbc
RUN apt-get install -y freetds-dev
RUN apt-get install -y tdsodbc
EXPOSE 80
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

To run docker docker run -it -v "$PWD":/app -p 1433:1433 -p 8000:8000 nmodes /bin/bas

Or docker-compose up see the docker-compose.yml

Now you will able to connect to your mssql on 192.168.65.1:1433

#django setting

DATABASES = {
   'default': {
       'ENGINE': 'django_pyodbc',  # ! you have to install it
       'NAME': 'your_DB',
       'HOST': '192.168.65.1',
       'USER': 'admin',
       'PASSWORD': 'your_password',
       'PORT': 1433,
       'OPTIONS': {
           'driver_needs_utf8': True,
           'decode_unicode': None,
           'client_encoding': 'utf-8',
           'driver': '/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so',
           'host_is_server': True,
           'extra_params': "TDS_VERSION=8.0",
           'autocommit': True,
       }
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment