Skip to content

Commit ac27dea

Browse files
author
hanif
committedApr 9, 2019
containerized
1 parent b3b17f2 commit ac27dea

File tree

8 files changed

+74
-83
lines changed

8 files changed

+74
-83
lines changed
 

‎Dockerfile-app

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.5-alpine
2+
3+
WORKDIR /app
4+
COPY source_code/ /app
5+
6+
RUN pip install -r requirements.txt
7+
8+
ENTRYPOINT ["python", "server.py"]

‎Dockerfile-mysql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM mysql:5.5
2+
3+
ENV MYSQL_DATABASE crud_flask
4+
ENV MYSQL_USER dev
5+
ENV MYSQL_PASSWORD dev
6+
ENV MYSQL_RANDOM_ROOT_PASSWORD yes
7+
8+
COPY database/ /docker-entrypoint-initdb.d/

‎README.md

+5-35
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,16 @@ A simple CRUD application using Flask and MySQL
88
* MySQL
99
* AdminLTE 2
1010

11-
### Running server.py
12-
```
13-
python3.5 server.py
14-
15-
```
16-
Open http://your-host-ip-address:8181 in browser.
17-
1811
### Running on Docker
1912

20-
Pull image:
21-
```
22-
docker pull muhammadhanif/crud-flask-mysql
23-
2413
```
25-
Run:
14+
docker-compose up -d
2615
```
27-
docker run -it --name crud-flask-mysql -p 80:80 muhammadhanif/crud-flask-mysql
28-
```
29-
Open http://your-host-ip-address in browser.
30-
31-
Or you can execute the following [docker-compose](https://raw.githubusercontent.com/muhammadhanif/crud-application-using-flask-and-mysql/master/docker-compose/docker-compose.yaml):
3216

33-
```
34-
version: '2'
35-
services:
17+
After executing, you will have 2 running cointainers on your Docker host: `phonebook-app` and `phonebook-mysql`. For accessing the web application, open your browser and go to http://your-docker-host-ip-address:8181
3618

37-
phonebook-mysql:
38-
container_name: phonebook-mysql
39-
image: muhammadhanif/phonebook:mysql
19+
To destroy the containers, execute:
4020

41-
phonebook-flask:
42-
container_name: phonebook-flask
43-
image: muhammadhanif/phonebook:flask
44-
ports:
45-
- "8181:8181"
46-
working_dir: /hnf/source_code
47-
command: python server.py
48-
links:
49-
- phonebook-mysql
5021
```
51-
52-
 
53-
After executing, you will have 2 running cointainers on your Docker host: phonebook-flask and phonebook-mysql. For accessing the web application, open your browser and go to http://your-docker-host-ip-address:8181
22+
docker-compose down --rmi all
23+
```

‎docker-compose.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '2'
2+
3+
services:
4+
phonebook-mysql:
5+
container_name: phonebook-mysql
6+
build:
7+
context: .
8+
dockerfile: Dockerfile-mysql
9+
restart: always
10+
11+
phonebook-app:
12+
container_name: phonebook-app
13+
build:
14+
context: .
15+
dockerfile: Dockerfile-app
16+
depends_on:
17+
- phonebook-mysql
18+
ports:
19+
- "8181:8181"
20+
restart: always

‎docker-compose/docker-compose.yaml

-17
This file was deleted.

‎source_code/module/database.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,68 @@
88

99
class Database:
1010
def connect(self):
11-
return pymysql.connect("localhost","dev","dev","crud_flask" )
12-
11+
return pymysql.connect("phonebook-mysql","dev","dev","crud_flask" )
12+
1313
def read(self, id):
1414
con = Database.connect(self)
1515
cursor = con.cursor()
16-
17-
try:
16+
17+
try:
1818
if id == None:
1919
cursor.execute("SELECT * FROM phone_book order by name asc")
20-
else:
20+
else:
2121
cursor.execute("SELECT * FROM phone_book where id = %s order by name asc", (id,))
2222

2323
return cursor.fetchall()
2424
except:
2525
return ()
2626
finally:
2727
con.close()
28-
28+
2929
def insert(self,data):
3030
con = Database.connect(self)
3131
cursor = con.cursor()
32-
32+
3333
try:
3434
cursor.execute("INSERT INTO phone_book(name,phone,address) VALUES(%s, %s, %s)", (data['name'],data['phone'],data['address'],))
3535
con.commit()
36-
36+
3737
return True
3838
except:
3939
con.rollback()
40-
40+
4141
return False
4242
finally:
4343
con.close()
44-
44+
4545
def update(self, id, data):
4646
con = Database.connect(self)
4747
cursor = con.cursor()
48-
48+
4949
try:
5050
cursor.execute("UPDATE phone_book set name = %s, phone = %s, address = %s where id = %s", (data['name'],data['phone'],data['address'],id,))
5151
con.commit()
52-
52+
5353
return True
5454
except:
5555
con.rollback()
56-
56+
5757
return False
5858
finally:
5959
con.close()
60-
60+
6161
def delete(self, id):
6262
con = Database.connect(self)
6363
cursor = con.cursor()
64-
64+
6565
try:
6666
cursor.execute("DELETE FROM phone_book where id = %s", (id,))
6767
con.commit()
68-
68+
6969
return True
7070
except:
7171
con.rollback()
72-
72+
7373
return False
7474
finally:
7575
con.close()

‎source_code/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flask
2+
pymysql

‎source_code/server.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@app.route('/')
1616
def index():
1717
data = db.read(None)
18-
18+
1919
return render_template('index.html', data = data)
2020

2121
@app.route('/add/')
@@ -29,15 +29,15 @@ def addphone():
2929
flash("A new phone number has been added")
3030
else:
3131
flash("A new phone number can not be added")
32-
32+
3333
return redirect(url_for('index'))
3434
else:
3535
return redirect(url_for('index'))
3636

3737
@app.route('/update/<int:id>/')
3838
def update(id):
3939
data = db.read(id);
40-
40+
4141
if len(data) == 0:
4242
return redirect(url_for('index'))
4343
else:
@@ -47,23 +47,23 @@ def update(id):
4747
@app.route('/updatephone', methods = ['POST'])
4848
def updatephone():
4949
if request.method == 'POST' and request.form['update']:
50-
50+
5151
if db.update(session['update'], request.form):
5252
flash('A phone number has been updated')
53-
53+
5454
else:
5555
flash('A phone number can not be updated')
56-
56+
5757
session.pop('update', None)
58-
58+
5959
return redirect(url_for('index'))
6060
else:
6161
return redirect(url_for('index'))
62-
62+
6363
@app.route('/delete/<int:id>/')
6464
def delete(id):
6565
data = db.read(id);
66-
66+
6767
if len(data) == 0:
6868
return redirect(url_for('index'))
6969
else:
@@ -73,15 +73,15 @@ def delete(id):
7373
@app.route('/deletephone', methods = ['POST'])
7474
def deletephone():
7575
if request.method == 'POST' and request.form['delete']:
76-
76+
7777
if db.delete(session['delete']):
7878
flash('A phone number has been deleted')
79-
79+
8080
else:
8181
flash('A phone number can not be deleted')
82-
82+
8383
session.pop('delete', None)
84-
84+
8585
return redirect(url_for('index'))
8686
else:
8787
return redirect(url_for('index'))
@@ -91,4 +91,4 @@ def page_not_found(error):
9191
return render_template('error.html')
9292

9393
if __name__ == '__main__':
94-
app.run(debug = True, port=8181, host="0.0.0.0")
94+
app.run(port=8181, host="0.0.0.0")

0 commit comments

Comments
 (0)
Please sign in to comment.