Repository: Radu-Raicea/Dockerized-Flask Branch: master Commit: cc7478c70985 Files: 25 Total size: 19.2 KB Directory structure: gitextract_x1ccivd5/ ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── docker-compose.yml ├── flask/ │ ├── Dockerfile │ ├── config.py │ ├── manage.py │ ├── project/ │ │ ├── __init__.py │ │ ├── controllers/ │ │ │ ├── __init__.py │ │ │ └── routes.py │ │ ├── models/ │ │ │ ├── __init__.py │ │ │ └── names.py │ │ ├── static/ │ │ │ ├── css/ │ │ │ │ └── style.css │ │ │ └── js/ │ │ │ └── script.js │ │ └── templates/ │ │ └── index.html │ ├── requirements.txt │ └── tests/ │ ├── __init__.py │ ├── test_configs.py │ └── test_website.py ├── nginx/ │ ├── Dockerfile │ ├── app.conf │ └── nginx.conf └── postgres/ ├── Dockerfile └── create.sql ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ ./ ../ .git/ .idea/ *.pyc flask/htmlcov/ .coverage ================================================ FILE: .travis.yml ================================================ sudo: required services: - docker env: DOCKER_COMPOSE_VERSION: 1.13.0 before_install: - sudo rm /usr/local/bin/docker-compose - curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose - chmod +x docker-compose - sudo mv docker-compose /usr/local/bin before_script: - docker-compose up --build -d - sleep 10 script: - docker-compose run flask python manage.py test after_script: - docker-compose down ================================================ FILE: LICENSE ================================================ BSD 3-Clause License Copyright (c) 2017, Radu Raicea All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================ FILE: README.md ================================================
Dockerized web app template using NGINX, Flask, and PostgreSQL.
. ├── LICENSE ├── README.md ├── docker-compose.yml ├── dockerized_logo.png ├── flask │ ├── Dockerfile │ ├── config.py │ ├── manage.py │ ├── project │ │ ├── __init__.py │ │ ├── controllers │ │ │ ├── __init__.py │ │ │ └── routes.py │ │ ├── models │ │ │ ├── __init__.py │ │ │ └── names.py │ │ ├── static │ │ │ ├── css │ │ │ ├── img │ │ │ └── js │ │ └── templates │ │ └── index.html │ ├── requirements.txt │ └── tests │ ├── __init__.py │ ├── test_configs.py │ └── test_website.py ├── nginx │ ├── Dockerfile │ ├── app.conf │ └── nginx.conf └── postgres ├── Dockerfile └── create.sql --- ## Installation * [Windows 10 (64-bit Pro)](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BInstallation%5D-Windows-10-Instructions-(64-bit-Pro)) * [Windows Toolbox](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BInstallation%5D-Windows-Instructions-(Toolbox)) * [macOS (Yosemite 10.10.3 and higher)](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BInstallation%5D-macOS-Instructions-(Yosemite-10.10.3-and-higher)) * [Linux (Ubuntu 16.04)](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BInstallation%5D-Linux-Instructions-(Ubuntu-16.04)) ## Flask * [Using Flask Script to run commands while application is running](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BFlask%5D-Using-Flask-Script-to-run-commands-while-the-application-is-running) * [Running unit tests with Flask Testing and coverage](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BFlask%5D-Running-unit-tests-with-Flask-Testing-and-coverage) ## Docker * [Remove all Docker volumes to delete the database](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BDocker%5D-Remove-all-Docker-volumes-to-delete-the-database) * [Access the PostgreSQL command line terminal through Docker](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BDocker%5D-Access-the-PostgreSQL-command-line-terminal-through-Docker) ## Other * [Access the PostgreSQL database using a 3rd party software](https://github.com/Radu-Raicea/Dockerized-Flask/wiki/%5BOther%5D-Access-the-PostgreSQL-database-using-a-3rd-party-software) ================================================ FILE: docker-compose.yml ================================================ # -------------------------------------------------------------------------- # When 'docker-compose up --build' is run, this file is executed. # # Its purpose is to run 3 containers (nginx, flask and postgres) and # attach them together in a common network with shared volumes. # -------------------------------------------------------------------------- version: '3' services: postgres: build: ./postgres container_name: postgres ports: - 2345:5432 networks: - net environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres volumes: - postgres:/var/lib/postgresql/data flask: build: ./flask container_name: flask volumes: - ./flask:/usr/src/app networks: - net environment: - APP_SETTINGS=config.DevelopmentConfig - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/db_dev - DATABASE_TEST_URL=postgresql://postgres:postgres@postgres:5432/db_test - SECRET_KEY=dockertutorial depends_on: - postgres links: - postgres command: gunicorn --worker-class eventlet -w 4 -b 0.0.0.0:8000 manage:app nginx: build: ./nginx container_name: nginx ports: - 80:80 restart: always networks: - net volumes: - ./flask/project/static:/usr/share/nginx/html/static depends_on: - flask volumes: postgres: networks: net: ================================================ FILE: flask/Dockerfile ================================================ # -------------------------------------------------------------------------- # When Docker builds the flask container, it builds it from this image. # # This file pulls a Python 3 image from Docker Hub (a sort of # GitHub for Docker images), and copies the requirements.txt file to the # container. It then installs all the Python dependencies from it. # -------------------------------------------------------------------------- FROM python:3.6.1 ENV PYTHONDONTWRITEBYTECODE=True RUN mkdir -p /usr/src/app WORKDIR /usr/src/app ADD ./requirements.txt /usr/src/app/requirements.txt RUN pip install -r requirements.txt ADD . /usr/src/app ================================================ FILE: flask/config.py ================================================ # -*- coding: utf-8 -*- """ This file stores all the possible configurations for the Flask app. Changing configurations like the secret key or the database url should be stored as environment variables and imported using the 'os' library in Python. """ import os class BaseConfig: SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = os.getenv('SECRET_KEY') DEBUG = False TESTING = False class TestingConfig(BaseConfig): SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_TEST_URL') DEBUG = True TESTING = True class DevelopmentConfig(BaseConfig): SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL') DEBUG = True class ProductionConfig(BaseConfig): SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL') DEBUG = False ================================================ FILE: flask/manage.py ================================================ # -*- coding: utf-8 -*- """ This is the entry point of the Flask application. """ import unittest import coverage from flask_script import Manager from project import create_app, logger, db # The logger should always be used instead of a print(). You need to import it from # the project package. If you want to understand how to use it properly and why you # should use it, check: http://bit.ly/2nqkupO logger.info('Server has started.') # Defines which parts of the code to include and omit when calculating code coverage. COV = coverage.coverage( branch=True, include='project/*', omit=[ 'tests/*', 'project/website/*' ] ) COV.start() # Creates the Flask application object that we use to initialize things in the app. app = create_app() # Creates all the models specified in project/models import project.models db.create_all(app=app) # Initializes the Manager object, which allows us to run terminal commands on the # Flask application while it's running (using Flask-Script). manager = Manager(app) @manager.command def cov(): """ Runs the unit tests and generates a coverage report on success. While the application is running, you can run the following command in a new terminal: 'docker-compose run --rm flask python manage.py cov' to run all the tests in the 'tests' directory. If all the tests pass, it will generate a coverage report. :return int: 0 if all tests pass, 1 if not """ tests = unittest.TestLoader().discover('tests') result = unittest.TextTestRunner(verbosity=2).run(tests) if result.wasSuccessful(): COV.stop() COV.save() print('Coverage Summary:') COV.report() COV.html_report() COV.erase() return 0 else: return 1 @manager.command def test(): """ Runs the unit tests without generating a coverage report. Enter 'docker-compose run --rm flask python manage.py test' to run all the tests in the 'tests' directory, with no coverage report. :return int: 0 if all tests pass, 1 if not """ tests = unittest.TestLoader().discover('tests', pattern='test*.py') result = unittest.TextTestRunner(verbosity=2).run(tests) if result.wasSuccessful(): return 0 else: return 1 @manager.command def test_one(test_file): """ Runs the unittest without generating a coverage report. Enter 'docker-compose run --rm flask python manage.py test_one