CS353 HW4 Tutorial
CS353 HW4 Tutorial
Description
• In this assignment, you are going to implement a generic Todo-list
application using Python, Flask, MySQL, and Docker Compose.
• To aid you in the coding process, we give you a sample application
that uses Python, Flask, MySQL and Docker Compose.
Logistics
Docker
• Docker is a virtualization platform that helps developers to easily
create, deploy, and run applications inside containers.
• Containers provide a consistent and isolated environment for
applications, ensuring that the applications can be replicated across
different environments.
Docker Compose
• Docker Compose is a tool for defining and running multi-container
Docker applications.
• You can define the services (in our case, web and db services) that
make up your application in a YAML file, and then start and stop all
services with a single command (We will show you how).
Flask
• Flask is a micro web framework written in Python.
• It is designed to be simple and lightweight,
allowing quick implementation and deployment of web applications
with minimal setup.
Installing Docker Compose on Windows
• On Windows, you can use the following link to obtain the installer:
• https://docs.docker.com/desktop/install/windows-install/
• It will install both Docker and Docker Compose for you.
Installing Docker Compose on Ubuntu Linux
• In Ubuntu, you can use the following link to install Docker Compose.
• https://www.digitalocean.com/community/tutorials/how-to-install-and-use-
docker-compose-on-ubuntu-20-04
• If you get the following, you need to start the Docker service
docker: error during connect: This error may indicate that the docker daemon is not running.: Post
"http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/create": open //./pipe/docker_engine: The
system cannot find the file specified.
See 'docker run --help'.
Sample Application
Application File Structure
• Simply extract the archive file we give you to obtain the following file
structure. You don’t need to change anything.
├── app
│ ├── app.py
│ └── templates
│ ├── login.html
│ └── register.html
├── docker-compose.yaml
├── Dockerfile
├── requirements.txt
└── schema.sql
├── app
│ ├── app.py • Application Directory app/
│ └── templates • Contains the main application file app.py
│ ├── login.html • HTML templates rendered by app.py
│ └── register.html
├── docker-compose.yaml
├── Dockerfile
├── requirements.txt
└── schema.sql
├── app
│ ├── app.py
│ └── templates
│ ├── login.html
│ └── register.html
├── docker-compose.yaml • Defines and combines the web and
database services
├── Dockerfile
├── requirements.txt
└── schema.sql
├── app
│ ├── app.py
│ └── templates
│ ├── login.html
│ └── register.html
├── docker-compose.yaml
• Defines how to web service is
├── Dockerfile constructed, what are its requirements,
├── requirements.txt which application files are going to be run
└── schema.sql
├── app
│ ├── app.py
│ └── templates
│ ├── login.html
│ └── register.html
├── docker-compose.yaml
├── Dockerfile
├── requirements.txt • Lists the required Python packages
└── schema.sql
├── app
│ ├── app.py
│ └── templates
│ ├── login.html
│ └── register.html
├── docker-compose.yaml
├── Dockerfile • The SQL Database Schema of the
application.
├── requirements.txt • Contains the tables. You can insert any
└── schema.sql SQL Statement here.
• Used to create and populate the database
when the services are started
automatically (Defined in
docker-compose.yaml file)
Dockerfile
• The Dockerfile below uses an existing python image and adds the python modules
defined in the requirements.txt file, which contains two modules Flask and
flask_mysqldb.
• It also points at the /app folder.
FROM python:3.9-slim-buster
RUN apt-get update
RUN apt-get install -y gcc
RUN apt-get install -y default-libmysqlclient-dev
WORKDIR /appCOPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
Building Your Container
• In the command line navigate to the folder where the Dockerfile
resides.
• Build your own docker image tagged cs353hw4app using:
docker build –t cs353hw4app .
• Once successful, you can list your images using
docker images
• You should see your image like the following:
REPOSITORY TAG IMAGE ID CREATED SIZE
cs353hw4app latest 603579517ecc 2 minutes ago 282MB
docker-compose.yaml
version: '3'
services:
web:
image: cs353hw4app Docker image name we just built
ports:
- "5000:5000" Web service port. We can reach our service at http://localhost:5000
volumes:
- ./app:/app app folder in the docker image mapped to app folder in our computer
working_dir: /app
command: python app.py running the app.py to start the service
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: password Mysql root password and database name
MYSQL_DATABASE: cs353hw4db
ports:
- "3307:3306"
volumes:
- Mapped schema.sql file. You need to prepare this file.
./schema.sql:/docker-entrypoint-initdb.d/schema.sql
Starting the services
• Before running with this configuration, you need to prepare your
schema.sql file that contains your database creation, table creation,
and insertion statements.
• Once you prepare your database schema, you can fire up the system
using
docker-compose up -d.
• You can view your web and database services by running
docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
import re
import os
from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
import MySQLdb.cursors
app = Flask(__name__)
app.secret_key = 'abcdefgh'
app.config['MYSQL_HOST'] = 'db'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'cs353hw4db'
mysql = MySQL(app)
Endpoints
• Currently the endpoints are
• /login
• /register
• You need to implement the other endpoints in accordance with the
functionality in the homework.
• Endpoints are implemented using the @app.route decorators in Flask.
Login Logic
• During grading we will only use the app files. We will not build the container image from
scratch for each submission. So, keep the following in mind:
• app folder (Do not change its name)
• app.py (Do not change its name)
• docker-compose.yaml (Do not change anything)
• Dockerfile (Do not change anything)
• schema.sql (Do not change the name, just fill it)
• requirements.txt (Do not change anything)