Oracle by Example brandingConnect a Python Application to Oracle Database Cloud Service

section 0Before You Begin

This 20-minute tutorial shows you how to build and package a simple Python application that connects to Oracle Database Cloud Service.

Background

Oracle Application Container Cloud Service lets you deploy Java SE, Node.js, PHP, and Python applications to the Oracle Cloud.

Official images pulled from DockerHub include just the language runtime but some applications need additional platform specific libraries installed. In addition to libraries, some Python modules also include platform specific components. As Oracle Application Container Cloud Service does not run deployed applications as root it is not possible to use Linux package installation utilities like dpkg-deb, apt-get, or yum in a startup script. This presents a challenge which this example shows how to overcome.

What Do You Need?


section 1Create a Python Application

  1. Create the python-oracle-app project directory in your local system.
  2. In the python-oracle-app directory, create the app.py file and add the following content:
    import os
    import cx_Oracle
    from flask import Flask
    
    db_user = os.environ.get('DBAAS_USER_NAME', 'SYSTEM')
    db_password = os.environ.get('DBAAS_USER_PASSWORD', 'Welcome1_')
    db_connect = os.environ.get('DBAAS_DEFAULT_CONNECT_DESCRIPTOR', "localhost:1521/ORCL")
    service_port = port=os.environ.get('PORT', '8080')
    
    app = Flask(__name__)
    
    @app.route('/')
    def index():
        connection = cx_Oracle.connect(db_user, db_password, db_connect)
        cur = connection.cursor()
        cur.execute("SELECT 'Hello, World from Oracle DB!' FROM DUAL")
        col = cur.fetchone()[0]
        cur.close()
        connection.close()
        return col
    
    if __name__ == '__main__':
          app.run(host='0.0.0.0', port= int(service_port) )
     
  3. Copy the libaio1_0.3.110-1_amd64.deb in the python-oracle-app directory.
  4. In the python-oracle-app directory, create the lib directory and extract there the content of the instantclient-basiclite-linux.x64-12.2.0.1.0.zip and instantclient-sdk-linux.x64-12.2.0.1.0.zip files.
  5. Create the requirements.txt file and add the project module dependencies and their versions.
    cx_Oracle==6.0b1 
    flask==0.12.1 

section 2Prepare Your Application for Cloud Deployment

  1. In the python-oracle-app directory, create the manifest.json file and add the following content:
    {
      "runtime": {
        "majorVersion": "3.6.0"
      },
      "command": "sh ./start.sh"
    } 
  2. Create the start.sh script file and add the following content:
    #!/bin/sh
    
    # Define PYTHONPATH as local modules folder
    export PYTHONPATH=${APP_HOME}/modules
    
    # Extract LIBAOI libs from Debian package (into ./lib/x86_64-linux-gnu)
    dpkg-deb -R libaio1_0.3.110-1_amd64.deb ${APP_HOME}
    
    # Finalize OCI installation by creating required softlink
    ln -s ${APP_HOME}/lib/instantclient_12_2/libclntsh.so.12.1 ${APP_HOME}/lib/instantclient_12_2/libclntsh.so
    
    # Add OCI and LIBAIO to shared library path
    export LD_LIBRARY_PATH=${APP_HOME}/lib/instantclient_12_2:${APP_HOME}/lib/x86_64-linux-gnu
    
    # Install Python packages into local modules folder
    pip --no-cache-dir install -r requirements.txt -t ${PYTHONPATH} --upgrade
    
    python ${APP_HOME}/app.py 

    Note: If you're using Windows to create this file, then make sure you convert line endings (EOL) to Unix format.

  3. Create the deploytment.json file and add the following content. Replace the placeholders with the credentials of your Oracle Database Cloud Service instance:
    {
      "services": [{
        "name": "<your-instance-name>",
        "type": "DBAAS",
        "username": "<your-db-username>",
        "password": "<your-db-user-password>" 
      }]
    } 
  4. Open a command-line (or terminal in Linux), go to the python-oracle-app directory, and create a zip file:
    zip -r python-orcl-app-dist.zip manifest.json app.py requirements.txt start.sh lib *.deb

section 3Deploy the Application to Oracle Application Container Cloud Service

  1. Open the Oracle Application Container Cloud Service console.
  2. In the Applications list view, click Create Application and select Python.
  3. If your account has access to Oracle Cloud Infrastructure regions, in the Region field select the same region than the region where your Oracle Database Cloud Service instance was created.
  4. In the Application section, enter a name for your application and click Browse.
  5. On the File Upload dialog box, select the python-orcl-app-dist.zip file you created in the previous section and click Open.
  6. Click More Options, then click Browse next to Deployment Configuration.
  7. On the File Upload dialog box, select the deployment.json file you created in the previous section and click Open.
  8. Keep the default values in the Instances and Memory fields and click Create.
  9. Wait until the application is created. The URL is enabled when the creation is completed.
  10. Click the URL of your application.
    HelloWorldApp response
    Description of the illustration python-app-test.png

more informationWant to Learn More?