This is the base of a web application in Flask to get you started and save time. The application run using gunicorn on a docker container thought a reverse proxy using NGINX and over HTTPS.
Docker multi-stage builds
Best practices for writing Dockerfiles
Larger Applications
Modular Applications with Blueprints
- NGINX
- Docker
- Docker Compose
- Bootstrap 4
- Normalize CSS
- Chart JS
- DataTables
- Font Awesome
- jQuery
- Modernizr
- Flask
- Flask-Login
- Flask-SQLAlchemy
- Flask-Bcrypt
- Flask-Security
- Flask-Uploads
- Flask-WTF
- WTForms
- email-validator
- gunicorn
- pyOpenSSL
The application comes with test certs for testing only. Make sure you do not use them on production.
/Flask-Boilerplate
/services
/reverse_proxy
/certs
cert.pem
key.pem
The credentials to login and test are:
Email: admin@example.local
Password: Admin123
/Flask-Boilerplate
.gitattributes
.gitignore
docker-compose.yml
LICENSE
README.md
/services
/reverse_proxy
Dockerfile
webapp.conf
/certs
/web
Dockerfile
/web_app
requirements.txt
wsgi.py
/myapp
__init__.py
forms.py
models.py
views.py
/blueprints
/static
/css
/js
/img
/vendors
/templates
$ python3 wsgi.py
NOTE: gunicorn does not work on Windows In order to run the application run it with gunicorn using the following command:
$ gunicorn -w 4 -b 0.0.0.0:5000 wsgi
Page title
{% block title %}
{% endblock title %}
CSS imports
{% block css_import %}
{% endblock css_import %}
CSS Style - Already has the "style" tags added, no need to add them.
{% block style %}
{% endblock style %}
Page content - The content block is right below page header inside the content area.
{% block content %}
{% endblock content %}
JS Imports
{% block js_import %}
{% endblock js_import %}
Script - Already has the "script" tags added, no need to add them.
{% block script %}
{% endblock script %}
Note: Run the following commands from the current directory where the Dockerfile is located.
Lets build everything using docker-compose with the switch -d to run it on the background and --build to build everything.
$ docker-compose up -d --build
To check the running containers
$ docker-compose ps
To login on the web_application container's shell
$ docker exec -ti web_application sh
To login on the reverse_proxy container's shell
$ docker exec -ti reverse_proxy sh
To inspect an image to see all the configs and how it is built.
$ docker image inspect web_application:web_application
To inspect an image to see all the configs and how it is built.
$ docker image inspect reverse_proxy:reverse_proxy
If you want to bring down the web application and remove the images run.
NOTE: This only removes the reverse proxy and web images
$ docker-compose down --volume --rmi all
To remove all images that are not being used by a running container
$ docker image prune -fa
To remove all volumes that are not being used by a running container
$ docker volume prune -f
To remove all networks that are not being used by a running container
$ docker network prune -f
To remove all containers that are not running
$ docker container prune -f
Sometimes you need to development an internal application for a company and the production server has no access to the outside world so you have to build the images in your local computer, export them, and then load them on the server.
Lets export our application images:
$ docker save -o web_application.tar web_application:web_application
$ docker save -o reverse_proxy.tar reverse_proxy:reverse_proxy
To load the image on another server use the following command:
$ docker load -i web_application.tar
$ docker load -i reverse_proxy.tar
Lets build a new docker-compose file and name it import-docker-compose.yml, Add the following code in the file.
version: '3.7'
services:
web_application:
restart: always
container_name: web_application
image: web_application:web_application
expose:
- 5000
entrypoint: ['gunicorn', '-w', '7', '-b', '0.0.0.0:5000', 'wsgi']
reverse_proxy:
restart: always
container_name: reverse_proxy
image: reverse_proxy:reverse_proxy
depends_on:
- web_application
ports:
- 443:443
- 80:80
Lets build the containers using our new import-docker-compose file.
$ docker-compose -f import-docker-compose.yml up -d
You can do so using ssl_context which create the certificates on the fly with the following code. NOTE: to be able to use ssl_context you need to install the following
$ pip install pyOpenSSL
from flask import Flask
application = Flask(__name__)
@application.route('/')
def home():
return 'Hello World!'
if __name__ == '__main__':
application.run(ssl_context='adhoc', host='0.0.0.0', port=443)
$ openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365