Skip to content

Commit 152243f

Browse files
authored
new version for Code Engine (#5)
* version for Code Engine * Db2 driver version * Db2 driver version * old CF branch
1 parent f5640e2 commit 152243f

8 files changed

Lines changed: 82 additions & 22 deletions

File tree

Dockerfile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
########
2+
# Python dependencies builder
3+
#
4+
# Full official Debian-based Python image
5+
FROM python:3.8 AS builder
6+
7+
# Always set a working directory
8+
WORKDIR /app
9+
# Sets utf-8 encoding for Python et al
10+
ENV LANG=C.UTF-8
11+
ENV PYTHONDONTWRITEBYTECODE=1
12+
ENV PYTHONUNBUFFERED=1
13+
14+
15+
# Ensures that the python and pip executables used
16+
# in the image will be those from our virtualenv.
17+
ENV PATH="/venv/bin:$PATH"
18+
19+
# Install OS package dependencies.
20+
# Do all of this in one RUN to limit final image size.
21+
RUN apt-get update && \
22+
apt-get install -y --no-install-recommends \
23+
build-essential && \
24+
rm -rf /var/lib/apt/lists/*
25+
26+
# Setup the virtualenv
27+
RUN python -m venv /venv
28+
29+
# Install Python deps
30+
COPY requirements.txt ./
31+
RUN pip install --no-cache-dir -r requirements.txt
32+
33+
34+
# Actual container
35+
#
36+
#
37+
FROM python:3.8-slim AS app
38+
39+
# Extra python env
40+
ENV PATH="/venv/bin:$PATH"
41+
42+
WORKDIR /app
43+
EXPOSE 8080
44+
45+
# copy in Python environment
46+
COPY --from=builder /venv /venv
47+
48+
RUN apt-get update && \
49+
apt-get install -y --no-install-recommends \
50+
libxml2 && \
51+
rm -rf /var/lib/apt/lists/*
52+
53+
# copy in the rest of the app
54+
COPY ./ ./
55+
ENTRYPOINT ["gunicorn", "--bind", "0.0.0.0:8080","worldcities:app"]

Procfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
**Note**: A former version of the tutorial deployed the app to Cloud Foundry Public on IBM Cloud. You can find the material in the [branch **cloudfoundry**](https://github.com/IBM-Cloud/cloud-sql-database/tree/cloudfoundry).
2+
13
# IBM Cloud SQL Database
24
This tutorial shows how to provision a SQL (relational) database service, create a table and load a larger data set, city information into the database. Thereafter, we deploy a web app "worldcities" to make use of that data and show how to access the cloud database. The app is written in Python using the Flask framework.
35

46
This tutorial is part of [IBM Cloud tutorials](https://cloud.ibm.com/docs/tutorials?topic=solution-tutorials-tutorials) and discussed as [SQL Database for Cloud Data](https://cloud.ibm.com/docs/solution-tutorials?topic=solution-tutorials-sql-database).
57

68
# Up and running in few steps
79
To get this SQL database-backed app up and running only few steps and about 10 minutes are needed. Please follow the steps outlined in the IBM Cloud tutorial.
8-
10+
11+
# Local testing
12+
13+
- Install the requirements to run Python directly or build and run the container image.
14+
- Set the environment variable **DASHDB_SSLDSN** to the value obtained from the Db2 Warehouse credentials for the key **ssldsn**.
15+
916
# Feedback
10-
If you have feedback on the code or the related tutorial, please either open an issue on this repository or leave documentation feedback at the above mentioned tutorial.
17+
If you have feedback on the code or the related tutorial, please open an issue on this repository.

manifest.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

requirements.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
Flask
2-
ibm_db
1+
Flask==2.0.3
2+
gunicorn
3+
ibm_db == 3.1.2
4+
python-dotenv==0.15.0

runtime.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% block title %}World Cities{% endblock %}
44
{% block content %}
55
<h1>Hello Cities of the World!</h1>
6-
<p>This app is named "{{ app["application_name"] }}". You can search for information on cities with a population over 1000. Use the local name.
6+
<p>You can search for information on cities with a population over 1000. Use the local name.
77
As an alternative, you can directly access city information using "/city/name", e.g., <a href="/city/Friedrichshafen">/city/Friedrichshafen</a>.</p>
88
<br>
99
<p>

worldcities.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# (C) 2017 IBM
1+
# (C) 2017-2022 IBM
22
# Author: Henrik Loeser
33
#
44
# Very short sample app used with Db2 Warehouse on Cloud to demonstrate
@@ -12,22 +12,27 @@
1212
import json
1313
import ibm_db
1414

15+
# for loading .env
16+
from dotenv import load_dotenv
17+
18+
# load environment
19+
load_dotenv()
20+
1521
app = Flask(__name__)
1622

1723
# get service information if on IBM Cloud Platform
18-
if 'VCAP_SERVICES' in os.environ:
19-
db2info = json.loads(os.environ['VCAP_SERVICES'])['dashDB'][0]
20-
db2cred = db2info["credentials"]
21-
appenv = json.loads(os.environ['VCAP_APPLICATION'])
24+
if 'DASHDB_SSLDSN' in os.environ:
25+
db2cred = os.getenv('DASHDB_SSLDSN')
2226
else:
23-
raise ValueError('Expected cloud environment')
27+
# log error, but continue - it might be before service binding
28+
app.logger.error('No Db2 credentials configured.')
2429

2530
# handle database request and query city information
2631
def city(name=None):
2732
# connect to DB2
2833
rows=[]
2934
try:
30-
db2conn = ibm_db.connect(db2cred['ssldsn'], "","")
35+
db2conn = ibm_db.connect(db2cred, "","")
3136
if db2conn:
3237
# we have a Db2 connection, query the database
3338
sql="select * from cities where name=? order by population desc"
@@ -55,7 +60,7 @@ def city(name=None):
5560
# main page to dump some environment information
5661
@app.route('/')
5762
def index():
58-
return render_template('index.html', app=appenv)
63+
return render_template('index.html')
5964

6065
# for testing purposes - use name in URI
6166
@app.route('/hello/<name>')

0 commit comments

Comments
 (0)