This article is contributed. See the original author and article here.

 


Pyodbc is an open-source Python module that helps in accessing ODBC database.



The following blog explains on how this module can be used for Linux based App Services.



  1. Using Blessed Image:
    The blessed image of Linux based App Service doesn’t have this module installed by default, as this is used for a specific business logic.

    The following steps helps in the installation of this module on blessed images:

    1. Deploy your code via Git repository or via local Git.

    2. The App Service deployment engine automatically activates  a virtual environment and runs pip install -r requirement.txt for you when you deploy to a Git repository or perform zip package deployment.

      You could refer to our documentation at Customize Build Automation which explains this in detail.




  2. Using Custom Image:
    You could build a custom image with pyodbc pre-installed.

    A sample image is available at vijaysaayi/Using-Pyodbc-in-Linux-App-Service (github.com)  


  The folder structure is as follows :


  vijaysaayi_0-1609313998942.jpeg


               


A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. This file is necessary when you’re creating a custom docker container image.


As you can see from the contents of my Dockerfile, I’ve mentioned the pre-requisites needed to install pyodbc: apt-get update and apt-get install -y –no-install-recommends build-essential gcc unixodbc-dev. Once the pre-requisites are installed, I install requirements.txt in my image. I also installed SSH on the image, since SSH needs to be specifically enabled on custom images.


 


02 - dockerfile.png


 


Here’s what my requirements.txt looks like:


vijaysaayi_2-1609313998981.jpeg


 


The entry point for my App is init.sh and does 2 things:



  1. Start the SSH Service

  2. Run main.py which starts a flask webserver
     vijaysaayi_3-1609313998983.jpeg


Now since pyodbc has been successfully installed already, it can be directly.


 


The following is my sample App Service that that return the first cell value from a given tableName


05 - main-py.png


 


To build this image, make sure you have the Docker Extension installed on Visual Studio Code. Once you have this installed, you can easily, build the image by Right click Dockerfile > Build Image.


I connected my VSCode to my Dockerhub account and pushed my image to my Dockerhub repository on hub.docker.com.


You can choose to push your image to Dockerhub or Azure Container Registry or a private registry of your choice.


vijaysaayi_5-1609313999004.jpeg



Here’s the link to the custom image pushed to DockerHub:


https://hub.docker.com/repository/docker/vijaysaayi/pyodbc-linux-appservice-demo


 


The following steps could help you deploy the above image to your Linux based App Service in case you would like to test it.



  1. Create an App Service Web App Container with the following configuration:
    07 - create new webapp.png
    07 - create new webapp - page 2.png


  2. Your DB connection string value in App Setting needs to be in the following format:
    DRIVER={ODBC Driver 17 for SQL Server};SERVER=’+server+’;DATABASE=’+database+’;UID=’+username+’;PWD=’+ password

  3. When you browse to the App Service , you should see the following response.  08 - sample response.png


               


 

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.