Set different networks for db-backend and backend-proxy

Signed-off-by: Anca Iordache <anca.iordache@docker.com>
master
Anca Iordache 5 years ago
parent 8717298d93
commit aed528ccf7

@ -1,5 +1,4 @@
FROM python:3.8-alpine FROM python:3.8-alpine
ENV PYTHONUNBUFFERED 1
WORKDIR /code WORKDIR /code
COPY requirements.txt /code/ COPY requirements.txt /code/
RUN pip install -r requirements.txt RUN pip install -r requirements.txt

@ -6,7 +6,8 @@ import mysql.connector
passfile = open('/run/secrets/db-password', 'r') passfile = open('/run/secrets/db-password', 'r')
#give db some time to start #give db some time to start
time.sleep(3) time.sleep(5)
#connect to db #connect to db
conn = mysql.connector.connect( conn = mysql.connector.connect(
user='root', user='root',
@ -15,12 +16,19 @@ conn = mysql.connector.connect(
database='example', database='example',
auth_plugin='mysql_native_password' auth_plugin='mysql_native_password'
) )
passfile.close()
passfile.close()
# populate db
cursor = conn.cursor() cursor = conn.cursor()
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()
prepare_db()
# server
app = Flask(__name__) app = Flask(__name__)
@app.route('/') @app.route('/')
def listBlog(): def listBlog():
cursor.execute('SELECT title FROM blog') cursor.execute('SELECT title FROM blog')
@ -29,13 +37,6 @@ def listBlog():
response = response + '<div>' + c[0] + '</div>' response = response + '<div>' + c[0] + '</div>'
return response 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__': if __name__ == '__main__':
prepare_db()
app.run() app.run()

@ -1,27 +1,40 @@
version: "3.7" version: "3.7"
services: services:
backend:
build: backend
secrets:
- db-password
ports:
- 5000:5000
db: db:
environment:
MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password
image: mysql:8.0.19 image: mysql:8.0.19
command: '--default-authentication-plugin=mysql_native_password'
restart: always restart: always
secrets: secrets:
- db-password - db-password
volumes: volumes:
- db-data:/var/lib/mysql - db-data:/var/lib/mysql
networks:
- backnet
environment:
- MYSQL_DATABASE=example
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/db-password
backend:
build: backend
restart: always
secrets:
- db-password
ports:
- 5000:5000
networks:
- backnet
- frontnet
proxy: proxy:
build: proxy build: proxy
restart: always
ports: ports:
- 8080:80 - 80:80
networks:
- frontnet
volumes: volumes:
db-data: {} db-data:
secrets: secrets:
db-password: db-password:
file: db/password.txt file: db/password.txt
networks:
backnet:
frontnet:

Loading…
Cancel
Save