diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b62f12 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +.vscode \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..039fe1b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM ubuntu:16.04 + +RUN apt-get update -y +RUN apt-get install -y python3 python3-pip python3-dev build-essential libssl-dev libffi-dev + +COPY . /app + +WORKDIR /app + +RUN pip3 install --upgrade pip +RUN pip3 install -r requirements.txt + +CMD ["python3", "run.py"] diff --git a/README.md b/README.md index baebcb7..ed2239d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,20 @@ This is a simple example of Python Flask's usage as a simple "Task Manager" usin ### Installation +#### Docker + +Using Docker compose, you can run this using one command: + +```bash +$ docker-compose up +``` + +It will run pip install and also pull mongodb container as requirement of this project. Then you can access `localhost:80` (yes it is in port 80 since in `docker-compose.yml` it exposed to host port 80, you can change it if you like!). + +#### Old way + +> Don't forget to install python 3 if you don't have it, because this project use python 3 syntax such as `print()` function. + First, you should [install MongoDB](https://docs.mongodb.com/manual/installation/) then install all dependencies by run the following command: @@ -28,7 +42,7 @@ then, run the program: $ python run.py ``` -Open your browser and go to `localhost:5000 ` to see the running program. +Open your browser and go to `localhost:5000` to see the running program. ### External References: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f4421db --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '2' +services: + mongo: + image: mongo:3.4 + volumes: + - /data/db + flask_mongodb: + build: . + ports: + - "80:5000" + environment: + - PORT=5000 + - MONGODB_URL=mongo:27017 + - SECRET_KEY=1234567890asdfghjkl + links: + - mongo + depends_on: + - mongo \ No newline at end of file diff --git a/run.py b/run.py index cf6730d..ce4c08d 100644 --- a/run.py +++ b/run.py @@ -1,11 +1,17 @@ +import os from flask import Flask, render_template, redirect from pymongo import MongoClient from classes import * +# get from env variable, follows 12 factor app (https://12factor.net/config) +SECRET_KEY = os.getenv('SECRET_KEY', 'randomkeys') +MONGODB_URL = os.getenv('MONGODB_URL', 'localhost:27017') +PORT = os.getenv('PORT', '5000') + # config system app = Flask(__name__) -app.config.update(dict(SECRET_KEY='yoursecretkey')) -client = MongoClient('localhost:27017') +app.config.update(dict(SECRET_KEY=SECRET_KEY)) +client = MongoClient(MONGODB_URL) db = client.TaskManager if db.settings.find({'name': 'task_id'}).count() <= 0: @@ -92,4 +98,10 @@ def main(): data = data, reset = reset) if __name__=='__main__': - app.run(debug=True) + # The web server running in your container is listening for connections on port 5000 + # #of the loopback network interface (127.0.0.1). As such this web server will only respond to http requests + # originating from that container itself. + # In order for the web server to accept connections originating from outside of the container + # you need to have it bind to the 0.0.0.0 IP address. + # see answer here https://stackoverflow.com/a/26434706/5489910 + app.run(host='0.0.0.0', port=PORT)