├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── app.py ├── requirements.txt ├── static └── style.css └── templates └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | .idea/ 103 | 104 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.6 2 | MAINTAINER Nar Kumar Chhantyal "nkchhantyal@gmail.com" 3 | RUN apt update && apt install python-dev -y 4 | COPY . /app 5 | WORKDIR /app 6 | RUN pip install -r requirements.txt 7 | EXPOSE 8000 8 | ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:8000", "--access-logfile", "-", "--error-logfile", "-"] 9 | CMD ["app:app"] 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2018, Nar Kumar Chhantyal 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flask-docker 2 | Fast and easy way to ship Python web apps, anywhere. Be shipping 🚀 3 | 4 | Building apps using Python is fun. 5 | But shipping them, not so much (unlike PHP & NodeJS, which are supported almost everywhere) 6 | However, Docker makes it really easy, here is how: 7 | 8 | - `Flask`: Web app framework. You could take any other WSGI framework 9 | - `Gunicorn`: Production grade App server for Python 10 | - `Whitenoise`: Serving static files (js, css, images etc) 11 | - `Docker`: Contenarize codebase + all of the above tech to ship 12 | 13 | 14 | ## Try 15 | 16 | You can try sample Flask app container from Docker Hub. 17 | For that, you don't need to clone this git source. 18 | 19 | - `docker pull chhantyal/flask-docker` 20 | - `docker run -p 8000:8000 flask-docker` 21 | 22 | ## Ship 23 | 24 | In few steps, you can run on local, your colleague's local, AWS, Azure, anywhere. 25 | 26 | Docker Container -> Container Registry -> Cloud 27 | 28 | First build container: 29 | * `docker build . -t flask-docker:latest` 30 | 31 | Test local container: 32 | * Run: `docker run -p 8000:8000 flask-docker:latest` 33 | * Open: `http://localhost:8000` 34 | 35 | If you want to deploy in the cloud: 36 | * Tag it: `docker tag flask-docker:latest container_registry.com/flask-docker:latest` 37 | * Push to remote container registry: `docker push container_registry.com/flask-docker:latest` 38 | * Run in remote server: `docker run -d -p 80:8000 flask-docker:latest` 39 | 40 | See `Dockerfile` ✨🍰✨ 41 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from flask import Flask, render_template 4 | from whitenoise import WhiteNoise 5 | 6 | 7 | app = Flask(__name__) 8 | app.secret_key = '[keep it secret]' 9 | static_dir = os.path.join(os.path.dirname(__file__), 'static') 10 | app.wsgi_app = WhiteNoise(app.wsgi_app, root=static_dir, prefix='static/') 11 | 12 | 13 | @app.route('/') 14 | def index(): 15 | return render_template('index.html') 16 | 17 | 18 | if __name__ == '__main__': 19 | app.jinja_env.auto_reload = True 20 | app.config['TEMPLATES_AUTO_RELOAD'] = True 21 | app.run(debug=True) 22 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask 2 | Whitenoise 3 | gunicorn 4 | -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | h1 { 2 | color: red; 3 | } -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |