forked from docker/awesome-compose
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connect backend to db in nginx-flask-mysql
Signed-off-by: Anca Iordache <anca.iordache@docker.com>
- Loading branch information
Anca Iordache
committed
Mar 17, 2020
1 parent
0b40d18
commit 92a9311
Showing
4 changed files
with
49 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
FROM python:3.6-alpine3.7 | ||
EXPOSE 5000 | ||
FROM python:3.8-alpine | ||
ENV PYTHONUNBUFFERED 1 | ||
RUN mkdir /code | ||
WORKDIR /code | ||
ADD requirements.txt /code/ | ||
COPY requirements.txt /code/ | ||
RUN pip install -r requirements.txt | ||
ADD . /code/ | ||
COPY . /code/ | ||
ENV FLASK_APP hello.py | ||
CMD flask run --host=0.0.0.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
import os | ||
import time | ||
from flask import Flask | ||
import mysql.connector | ||
|
||
passfile = open('/run/secrets/db-password', 'r') | ||
|
||
#give db some time to start | ||
time.sleep(3) | ||
#connect to db | ||
conn = mysql.connector.connect( | ||
user='root', | ||
password=passfile.read(), | ||
host='db', # name of the mysql service as set in the docker-compose file | ||
database='example', | ||
auth_plugin='mysql_native_password' | ||
) | ||
passfile.close() | ||
|
||
cursor = conn.cursor() | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def hello_world(): | ||
return 'Hello world' | ||
def listBlog(): | ||
cursor.execute('SELECT title FROM blog') | ||
response = '' | ||
for c in cursor: | ||
response = response + '<div>' + c[0] + '</div>' | ||
return response | ||
|
||
def prepare_db(): | ||
cursor.execute('DROP TABLE IF EXISTS blog') | ||
cursor.execute('CREATE TABLE blog (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255))') | ||
cursor.executemany('INSERT INTO blog (id, title) VALUES (%s, %s);', [(i, 'Blog post #%d'% i) for i in range (1,5)]) | ||
conn.commit() | ||
|
||
|
||
if __name__ == '__main__': | ||
prepare_db() | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
click==6.7 | ||
Flask==1.0.2 | ||
itsdangerous==0.24 | ||
Jinja2==2.10 | ||
MarkupSafe==1.0 | ||
Werkzeug==0.14.1 | ||
Flask==1.1.1 | ||
mysql-connector==2.2.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters