1st
This commit is contained in:
parent
916bd8bcb7
commit
11a4c2c01e
11
z1/README.md
Normal file
11
z1/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
## Dockerizing Flask With Compose and Machine - From Localhost to the Cloud
|
||||
|
||||
Featuring:
|
||||
|
||||
- Docker v18.09.2
|
||||
- Docker Compose v1.23.2
|
||||
- Docker Machine v0.16.1
|
||||
|
||||
**Check out the awesome blog post here > https://realpython.com/blog/python/dockerizing-flask-with-compose-and-machine-from-localhost-to-the-cloud/**
|
||||
|
||||
Cheers!
|
46
z1/docker-compose.yml
Normal file
46
z1/docker-compose.yml
Normal file
@ -0,0 +1,46 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
web:
|
||||
restart: always
|
||||
build: ./web
|
||||
expose:
|
||||
- "8000"
|
||||
links:
|
||||
- postgres:postgres
|
||||
volumes:
|
||||
- web-data:/usr/src/app/static
|
||||
env_file:
|
||||
- .env
|
||||
command: /usr/local/bin/gunicorn -w 2 -b :8000 app:app
|
||||
|
||||
nginx:
|
||||
restart: always
|
||||
build: ./nginx
|
||||
ports:
|
||||
- "80:80"
|
||||
volumes:
|
||||
- .:/www/static
|
||||
- web-data:/usr/src/app/static
|
||||
links:
|
||||
- web:web
|
||||
|
||||
data:
|
||||
image: postgres:latest
|
||||
volumes:
|
||||
- db-data:/var/lib/postgresql/data
|
||||
command: "true"
|
||||
|
||||
postgres:
|
||||
restart: always
|
||||
image: postgres:latest
|
||||
environment:
|
||||
- POSTGRES_HOST_AUTH_METHOD=trust
|
||||
volumes:
|
||||
- db-data:/var/lib/postgresql/data
|
||||
ports:
|
||||
- "5432:5432"
|
||||
|
||||
volumes:
|
||||
db-data:
|
||||
web-data:
|
5
z1/nginx/Dockerfile
Normal file
5
z1/nginx/Dockerfile
Normal file
@ -0,0 +1,5 @@
|
||||
FROM tutum/nginx
|
||||
|
||||
RUN rm /etc/nginx/sites-enabled/default
|
||||
|
||||
COPY sites-enabled/ /etc/nginx/sites-enabled
|
17
z1/nginx/sites-enabled/flask_project
Normal file
17
z1/nginx/sites-enabled/flask_project
Normal file
@ -0,0 +1,17 @@
|
||||
server {
|
||||
|
||||
listen 80;
|
||||
server_name example.org;
|
||||
charset utf-8;
|
||||
|
||||
location /static {
|
||||
alias /usr/src/app/static;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://web:8000;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
}
|
||||
}
|
10
z1/web/Dockerfile
Normal file
10
z1/web/Dockerfile
Normal file
@ -0,0 +1,10 @@
|
||||
FROM python:3.7-slim
|
||||
|
||||
RUN python -m pip install --upgrade pip
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN python -m pip install -r requirements.txt
|
||||
|
||||
COPY . .
|
30
z1/web/app.py
Normal file
30
z1/web/app.py
Normal file
@ -0,0 +1,30 @@
|
||||
# app.py
|
||||
|
||||
|
||||
from flask import Flask
|
||||
from flask import request, render_template
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from config import BaseConfig
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(BaseConfig)
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
|
||||
from models import *
|
||||
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
if request.method == 'POST':
|
||||
text = request.form['text']
|
||||
post = Post(text)
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
posts = Post.query.order_by(Post.date_posted.desc()).all()
|
||||
return render_template('index.html', posts=posts)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
28
z1/web/config.py
Normal file
28
z1/web/config.py
Normal file
@ -0,0 +1,28 @@
|
||||
# config.py
|
||||
|
||||
|
||||
import os
|
||||
|
||||
|
||||
class BaseConfig(object):
|
||||
SECRET_KEY = os.environ['SECRET_KEY']
|
||||
DEBUG = os.environ['DEBUG']
|
||||
DB_NAME = os.environ['DB_NAME']
|
||||
DB_USER = os.environ['DB_USER']
|
||||
DB_PASS = os.environ['DB_PASS']
|
||||
DB_SERVICE = os.environ['DB_SERVICE']
|
||||
DB_PORT = os.environ['DB_PORT']
|
||||
SQLALCHEMY_DATABASE_URI = 'postgresql://{0}:{1}@{2}:{3}/{4}'.format(
|
||||
DB_USER, DB_PASS, DB_SERVICE, DB_PORT, DB_NAME
|
||||
)
|
||||
|
||||
|
||||
# class BaseConfig(object):
|
||||
# SECRET_KEY = 'hi'
|
||||
# DEBUG = True
|
||||
# DB_NAME = 'postgres'
|
||||
# DB_SERVICE = 'localhost'
|
||||
# DB_PORT = 5432
|
||||
# SQLALCHEMY_DATABASE_URI = 'postgresql://{0}:{1}/{2}'.format(
|
||||
# DB_SERVICE, DB_PORT, DB_NAME
|
||||
# )
|
6
z1/web/create_db.py
Normal file
6
z1/web/create_db.py
Normal file
@ -0,0 +1,6 @@
|
||||
# create_db.py
|
||||
|
||||
|
||||
from app import db
|
||||
|
||||
db.create_all()
|
18
z1/web/models.py
Normal file
18
z1/web/models.py
Normal file
@ -0,0 +1,18 @@
|
||||
# models.py
|
||||
|
||||
|
||||
import datetime
|
||||
from app import db
|
||||
|
||||
|
||||
class Post(db.Model):
|
||||
|
||||
__tablename__ = 'posts'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
text = db.Column(db.String, nullable=False)
|
||||
date_posted = db.Column(db.DateTime, nullable=False)
|
||||
|
||||
def __init__(self, text):
|
||||
self.text = text
|
||||
self.date_posted = datetime.datetime.now()
|
9
z1/web/requirements.txt
Normal file
9
z1/web/requirements.txt
Normal file
@ -0,0 +1,9 @@
|
||||
Flask==1.0.2
|
||||
Flask-SQLAlchemy==2.3.2
|
||||
Jinja2==2.10
|
||||
MarkupSafe==1.1.1
|
||||
SQLAlchemy==1.3.1
|
||||
Werkzeug==0.15.1
|
||||
gunicorn==19.9.0
|
||||
itsdangerous==1.1.0
|
||||
psycopg2==2.7.7
|
5
z1/web/static/css/bootstrap.min.css
vendored
Normal file
5
z1/web/static/css/bootstrap.min.css
vendored
Normal file
File diff suppressed because one or more lines are too long
5
z1/web/static/css/main.css
Normal file
5
z1/web/static/css/main.css
Normal file
@ -0,0 +1,5 @@
|
||||
/* custom styles */
|
||||
|
||||
.container {
|
||||
max-width: 500px;
|
||||
}
|
1
z1/web/static/img/.gitkeep
Normal file
1
z1/web/static/img/.gitkeep
Normal file
@ -0,0 +1 @@
|
||||
|
7
z1/web/static/js/bootstrap.min.js
vendored
Normal file
7
z1/web/static/js/bootstrap.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
z1/web/static/js/main.js
Normal file
1
z1/web/static/js/main.js
Normal file
@ -0,0 +1 @@
|
||||
// custom scripts
|
27
z1/web/templates/_base.html
Normal file
27
z1/web/templates/_base.html
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Flask on Docker</title>
|
||||
<!-- meta -->
|
||||
<meta name="description" content="">
|
||||
<meta name="author" content="">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<!-- styles -->
|
||||
<link href="static/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="static/css/main.css" rel="stylesheet">
|
||||
{% block css %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<br>
|
||||
<!-- child template -->
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
<!-- scripts -->
|
||||
<script src="//code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script>
|
||||
<script src="static/js/bootstrap.min.js" type="text/javascript"></script>
|
||||
<script src="static/js/main.js" type="text/javascript"></script>
|
||||
{% block js %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
27
z1/web/templates/index.html
Normal file
27
z1/web/templates/index.html
Normal file
@ -0,0 +1,27 @@
|
||||
{% extends "_base.html" %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="text-center">
|
||||
<h1>Welcome!</h1>
|
||||
</div>
|
||||
|
||||
<br>
|
||||
|
||||
<form action="/" method="POST">
|
||||
<div class="form-group">
|
||||
<input name="text" class="form-control input-lg" type="text" placeholder="Enter a message" required>
|
||||
</div>
|
||||
<input type="submit" class="btn btn-primary btn-lg btn-block" value="Submit">
|
||||
</form>
|
||||
|
||||
<br>
|
||||
<hr>
|
||||
|
||||
<div>
|
||||
{% for post in posts %}
|
||||
<h4 class="well"><strong>{{post.text}}</strong> - <em>{{post.date_posted.strftime('%Y-%m-%d')}}</em></h4>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user