Skip to content

Instantly share code, notes, and snippets.

@mfullen
Last active August 10, 2017 20:44
Show Gist options
  • Save mfullen/071318df47860f6f51502288fc141667 to your computer and use it in GitHub Desktop.
Save mfullen/071318df47860f6f51502288fc141667 to your computer and use it in GitHub Desktop.
cs6400-dbsys Docker LAMP stack
version: "3.3"
services:
mysql:
#image: mysql:5.6.26
build: ./sql
container_name: cs6400-mysql
ports:
- 3306:3306
environment:
MYSQL_USER: "cs6400u"
MYSQL_PASSWORD: "password"
MYSQL_ROOT_PASSWORD: "mypassword"
MYSQL_DATABASE: "cs6400"
volumes:
- db-data:/var/lib/mysql/data
restart: always
networks:
- overlay
web:
build: ./web
depends_on:
- mysql
container_name: web
ports:
- 4000:8080
volumes:
- .:/code
links:
- mysql:mysql
restart: always
networks:
- overlay
volumes:
db-data:
networks:
overlay:
FROM mysql:5.6.26
ADD example-schema.sql /docker-entrypoint-initdb.d/
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
INSERT INTO Persons(`PersonID`, `LastName`, `FirstName`, `Address`, `City`)
values (1, "Wayne", "Bruce", "Wayne Manor", "Gotham");
INSERT INTO Persons(`PersonID`, `LastName`, `FirstName`, `Address`, `City`)
values (2, "Parker", "Peter", "123 Main Street", "New York");
INSERT INTO Persons(`PersonID`, `LastName`, `FirstName`, `Address`, `City`)
values (3, "Kent", "Clark", "Farmland", "Metropolis");
from flask import Flask, render_template
from flaskext.mysql import MySQL
import os
import socket
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'mypassword'
app.config['MYSQL_DATABASE_DB'] = 'cs6400'
#app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_HOST'] = 'mysql' #mysql is the name of the docker container
mysql.init_app(app)
@app.route("/")
def hello():
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}" \
"<a href='/myindex'>Load from DB </a>"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
@app.route("/myindex")
def myindex():
cursor = mysql.connect().cursor()
cursor.execute("SELECT * from Persons")
data = cursor.fetchall()
return render_template('index.html', data=data)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
# Use an official Python runtime as a parent image
FROM python:2.7-slim
RUN apt-get update && apt-get install -y gcc python-mysqldb libmysqlclient-dev
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Flask
MySQL-python
flask-mysql
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example Python Flask App</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="header">
<!--<nav>-->
<!--<ul class="nav nav-pills pull-right">-->
<!--<li role="presentation" class="active"><a href="#">Home</a>-->
<!--</li>-->
<!--<li role="presentation"><a href="#">Sign In</a>-->
<!--</li>-->
<!--<li role="presentation"><a href="showSignUp">Sign Up</a>-->
<!--</li>-->
<!--</ul>-->
<!--</nav>-->
<h3 class="text-muted">Python Flask App</h3>
</div>
<div>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Last Name</th>
<th>First Name</th>
<th>Address</th>
<th>City</th>
</tr>
</thead>
{% for row in data %}
<tr>
{% for d in row %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
<footer class="footer">
<p>&copy; Company 2015</p>
</footer>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment