Merge pull request #14 from aiordache/master
Connect backend to db in the nginx-flask-mysql sample
This commit is contained in:
		| @@ -1,10 +1,9 @@ | |||||||
| FROM python:3.6-alpine3.7 | FROM python:3.8-alpine | ||||||
| EXPOSE 5000 |  | ||||||
| ENV PYTHONUNBUFFERED 1 | ENV PYTHONUNBUFFERED 1 | ||||||
| RUN mkdir /code |  | ||||||
| WORKDIR /code | WORKDIR /code | ||||||
| ADD requirements.txt /code/ | COPY requirements.txt /code/ | ||||||
| RUN pip install -r requirements.txt | RUN pip install -r requirements.txt | ||||||
| ADD . /code/ | COPY . /code/ | ||||||
| ENV FLASK_APP hello.py | ENV FLASK_APP hello.py | ||||||
| CMD flask run --host=0.0.0.0 | CMD flask run --host=0.0.0.0 | ||||||
|  |   | ||||||
| @@ -1,6 +1,41 @@ | |||||||
|  | import os | ||||||
|  | import time | ||||||
| from flask import Flask | 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 = Flask(__name__) | ||||||
|  |  | ||||||
| @app.route('/') | @app.route('/') | ||||||
| def hello_world(): | def listBlog(): | ||||||
|     return 'Hello world' |     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() | ||||||
|   | |||||||
| @@ -1,6 +1,2 @@ | |||||||
| click==6.7 | Flask==1.1.1 | ||||||
| Flask==1.0.2 | mysql-connector==2.2.9 | ||||||
| itsdangerous==0.24 |  | ||||||
| Jinja2==2.10 |  | ||||||
| MarkupSafe==1.0 |  | ||||||
| Werkzeug==0.14.1 |  | ||||||
|   | |||||||
| @@ -2,13 +2,15 @@ version: "3.7" | |||||||
| services: | services: | ||||||
|   backend: |   backend: | ||||||
|     build: backend |     build: backend | ||||||
|     depends_on:  |     secrets: | ||||||
|     - db |       - db-password | ||||||
|  |     ports: | ||||||
|  |       - 5000:5000 | ||||||
|   db: |   db: | ||||||
|     environment: |     environment: | ||||||
|       MYSQL_DATABASE: example |       MYSQL_DATABASE: example | ||||||
|       MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password |       MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password | ||||||
|     image: mysql:5.7 |     image: mysql:8.0.19 | ||||||
|     restart: always |     restart: always | ||||||
|     secrets: |     secrets: | ||||||
|     - db-password |     - db-password | ||||||
| @@ -17,9 +19,7 @@ services: | |||||||
|   proxy: |   proxy: | ||||||
|     build: proxy |     build: proxy | ||||||
|     ports: |     ports: | ||||||
|     - 80:80 |     - 8080:80 | ||||||
|     depends_on:  |  | ||||||
|     - backend |  | ||||||
| volumes: | volumes: | ||||||
|   db-data: {} |   db-data: {} | ||||||
| secrets: | secrets: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user