From 92a9311d8aba3bb53254d17cd7173acba4edf288 Mon Sep 17 00:00:00 2001 From: Anca Iordache Date: Tue, 17 Mar 2020 21:55:32 +0100 Subject: [PATCH 1/2] connect backend to db in nginx-flask-mysql Signed-off-by: Anca Iordache --- nginx-flask-mysql/backend/Dockerfile | 9 +++-- nginx-flask-mysql/backend/hello.py | 39 ++++++++++++++++++++-- nginx-flask-mysql/backend/requirements.txt | 8 ++--- nginx-flask-mysql/docker-compose.yaml | 12 +++---- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/nginx-flask-mysql/backend/Dockerfile b/nginx-flask-mysql/backend/Dockerfile index 190d518..5faf0fe 100755 --- a/nginx-flask-mysql/backend/Dockerfile +++ b/nginx-flask-mysql/backend/Dockerfile @@ -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 + \ No newline at end of file diff --git a/nginx-flask-mysql/backend/hello.py b/nginx-flask-mysql/backend/hello.py index a60da8d..20f3010 100755 --- a/nginx-flask-mysql/backend/hello.py +++ b/nginx-flask-mysql/backend/hello.py @@ -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 + '
' + c[0] + '
' + 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() diff --git a/nginx-flask-mysql/backend/requirements.txt b/nginx-flask-mysql/backend/requirements.txt index 34ffbb7..d6c7773 100755 --- a/nginx-flask-mysql/backend/requirements.txt +++ b/nginx-flask-mysql/backend/requirements.txt @@ -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 diff --git a/nginx-flask-mysql/docker-compose.yaml b/nginx-flask-mysql/docker-compose.yaml index a9f740f..a30d724 100644 --- a/nginx-flask-mysql/docker-compose.yaml +++ b/nginx-flask-mysql/docker-compose.yaml @@ -2,13 +2,15 @@ version: "3.7" services: backend: build: backend - depends_on: - - db + secrets: + - db-password + ports: + - 5000:5000 db: environment: MYSQL_DATABASE: example MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password - image: mysql:5.7 + image: mysql:8.0.19 restart: always secrets: - db-password @@ -17,9 +19,7 @@ services: proxy: build: proxy ports: - - 80:80 - depends_on: - - backend + - 8080:80 volumes: db-data: {} secrets: From a7e0f380f64cee0935bcbf06298882a7a4135273 Mon Sep 17 00:00:00 2001 From: Anca Iordache Date: Tue, 17 Mar 2020 22:03:44 +0100 Subject: [PATCH 2/2] update mysql version and query output --- nginx-flask-mysql/README.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/nginx-flask-mysql/README.md b/nginx-flask-mysql/README.md index 9f7060f..069d418 100644 --- a/nginx-flask-mysql/README.md +++ b/nginx-flask-mysql/README.md @@ -21,14 +21,14 @@ services: build: backend ... db: - image: mysql:5.7 + image: mysql:8.0.19 ... proxy: build: proxy ... ``` The compose file defines an application with three services `proxy`, `backend` and `db`. -When deploying the application, docker-compose maps port 80 of the proxy service container to port 80 of the host as specified in the file. +When deploying the application, docker-compose maps port 80 of the proxy service container to port 8080 of the host as specified in the file. Make sure port 80 on the host is not already being in use. ## Deploy with docker-compose @@ -36,7 +36,7 @@ Make sure port 80 on the host is not already being in use. ``` $ docker-compose up -d Creating network "nginx-flask-mysql_default" with the default driver -Pulling db (mysql:5.7)... +Pulling db (mysql:8.0.19)... 5.7: Pulling from library/mysql ... ... @@ -51,16 +51,18 @@ Creating nginx-flask-mysql_proxy_1 ... done Listing containers must show two containers running and the port mapping as below: ``` $ docker ps -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -c65ecef87e85 nginx-flask-mysql_proxy "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-flask-mysql_proxy_1 -96ccc0a5342f nginx-flask-mysql_backend "/bin/sh -c 'flask r…" About a minute ago Up About a minute 5000/tcp nginx-flask-mysql_backend_1 -39327313a142 mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 3306/tcp, 33060/tcp nginx-flask-mysql_db_1 +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +c2c703b66b19 nginx-flask-mysql_proxy "nginx -g 'daemon of…" 39 seconds ago Up 38 seconds 0.0.0.0:8080->80/tcp nginx-flask-mysql_proxy_1 +2b8a21508c3c nginx-flask-mysql_backend "/bin/sh -c 'flask r…" 9 minutes ago Up 38 seconds 0.0.0.0:5000->5000/tcp nginx-flask-mysql_backend_1 +0e6a96ea2028 mysql:8.0.19 "docker-entrypoint.s…" 9 minutes ago Up 38 seconds 3306/tcp, 33060/tcp nginx-flask-mysql_db_1 + + ``` After the application starts, navigate to `http://localhost:80` in your web browser or run: ``` -$ curl localhost:80 -Hello world +$ curl localhost:8080 +
Blog post #1
Blog post #2
Blog post #3
Blog post #4
``` Stop and remove the containers