Before 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?
- Access to an instance of Oracle Application Container Cloud Service
- An instance and credentials of Oracle Database Cloud Service. See Oracle Database Cloud - Database as a Service Quick Start
- The Instant Client Package - Basic Light: instantclient-basiclite-linux.x64-12.2.0.1.0.zip
- The Instant Client Package - JDBC Supplement: instantclient-sdk-linux.x64-12.2.0.1.0.zip
- The Debian libaio package libaio1_0.3.110-1_amd64.deb
Create a Python Application
- Create the
python-oracle-app
project directory in your local system. - In the
python-oracle-app
directory, create theapp.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) )
- Copy the
libaio1_0.3.110-1_amd64.deb
in thepython-oracle-app
directory. - In the
python-oracle-app
directory, create thelib
directory and extract there the content of theinstantclient-basiclite-linux.x64-12.2.0.1.0.zip
andinstantclient-sdk-linux.x64-12.2.0.1.0.zip
files. - Create the
requirements.txt
file and add the project module dependencies and their versions.cx_Oracle==6.0b1 flask==0.12.1
Prepare Your Application for Cloud Deployment
- In the
python-oracle-app
directory, create themanifest.json
file and add the following content:{ "runtime": { "majorVersion": "3.6.0" }, "command": "sh ./start.sh" }
- 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.
- 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>" }] }
- 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
Deploy the Application to Oracle Application
Container Cloud Service
- Open the Oracle Application Container Cloud Service console.
- In the Applications list view, click Create Application and select Python.
- 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.
- In the Application section, enter a name for your application and click Browse.
- On the File Upload dialog box, select the
python-orcl-app-dist.zip
file you created in the previous section and click Open. - Click More Options, then click Browse next to Deployment Configuration.
- On the File Upload dialog box, select the
deployment.json
file you created in the previous section and click Open. - Keep the default values in the Instances and Memory fields and click Create.
- Wait until the application is created. The URL is enabled when the creation is completed.
- Click the URL of your application.
Description of the illustration python-app-test.png
Want to Learn More?
- Oracle Application Container Cloud Service in the Oracle Help Center
- Connect a Python Application to Oracle MySQL Cloud Service tutorial