From 6e69c4a5d0a472cee89c5efb187f6cca6aa8abde Mon Sep 17 00:00:00 2001 From: Rahul Vaish Date: Sat, 9 Mar 2019 12:40:29 +0530 Subject: [PATCH 1/3] Add files via upload --- FlaskWebServiceCloud/Dockerfile | 18 +++++++++++ .../my_project/api/__init__.py | 1 + .../api/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 185 bytes .../api/__pycache__/views.cpython-37.pyc | Bin 0 -> 591 bytes .../my_project/api/resources/calculator.py | 28 ++++++++++++++++++ .../my_project/api/resources/greetings.py | 22 ++++++++++++++ FlaskWebServiceCloud/my_project/api/views.py | 25 ++++++++++++++++ FlaskWebServiceCloud/my_project/app.py | 24 +++++++++++++++ FlaskWebServiceCloud/my_project/configwsgi.py | 17 +++++++++++ FlaskWebServiceCloud/my_project/run.sh | 1 + FlaskWebServiceCloud/my_project/wsgi.py | 6 ++++ FlaskWebServiceCloud/requirements.txt | 4 +++ 12 files changed, 146 insertions(+) create mode 100644 FlaskWebServiceCloud/Dockerfile create mode 100644 FlaskWebServiceCloud/my_project/api/__init__.py create mode 100644 FlaskWebServiceCloud/my_project/api/__pycache__/__init__.cpython-37.pyc create mode 100644 FlaskWebServiceCloud/my_project/api/__pycache__/views.cpython-37.pyc create mode 100644 FlaskWebServiceCloud/my_project/api/resources/calculator.py create mode 100644 FlaskWebServiceCloud/my_project/api/resources/greetings.py create mode 100644 FlaskWebServiceCloud/my_project/api/views.py create mode 100644 FlaskWebServiceCloud/my_project/app.py create mode 100644 FlaskWebServiceCloud/my_project/configwsgi.py create mode 100644 FlaskWebServiceCloud/my_project/run.sh create mode 100644 FlaskWebServiceCloud/my_project/wsgi.py create mode 100644 FlaskWebServiceCloud/requirements.txt diff --git a/FlaskWebServiceCloud/Dockerfile b/FlaskWebServiceCloud/Dockerfile new file mode 100644 index 0000000..b516019 --- /dev/null +++ b/FlaskWebServiceCloud/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.6.7-alpine3.8 + +ENV workdir="/usr/src/app" + +WORKDIR $workdir + +COPY ./my_project $workdir +COPY ./requirements.txt $workdir + +RUN apk update +RUN apk add nginx +RUN pip install --upgrade pip +RUN pip install -r requirements.txt +RUN chmod 555 run.sh + +EXPOSE 5000 + +CMD ./run.sh diff --git a/FlaskWebServiceCloud/my_project/api/__init__.py b/FlaskWebServiceCloud/my_project/api/__init__.py new file mode 100644 index 0000000..14cd5bd --- /dev/null +++ b/FlaskWebServiceCloud/my_project/api/__init__.py @@ -0,0 +1 @@ +from . import views diff --git a/FlaskWebServiceCloud/my_project/api/__pycache__/__init__.cpython-37.pyc b/FlaskWebServiceCloud/my_project/api/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dd68e6ab4d0c7a9aa843c1a33d6a967775bfe6c8 GIT binary patch literal 185 zcmZ?d<>g`kg8mtGF(N?vF^B^LOhASM5Elyoi4=wu#vF!R#wbQch7_h?22JLdj6h*c z##^jqnW^Q)ews|T7>YnzRx%W^04Xr>%f-bi#w9;FJGICyKPM%%D8{`gHMJx>zbGfg zEhn)!J3KWhIJKxOGdVRTw=%wTZlX-=vg K$jV|MW&i-R@-DIf literal 0 HcmV?d00001 diff --git a/FlaskWebServiceCloud/my_project/api/__pycache__/views.cpython-37.pyc b/FlaskWebServiceCloud/my_project/api/__pycache__/views.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..09c3805e1d21f7cdd9fe8aca78772952754c25ae GIT binary patch literal 591 zcmZ8d!EV$r5Vhk?HpwP|1AGJ8OCzpTRT0z{Dxs>Pph{MxD8$okwoV+`$u7;2dnG;r zj(i0YpOGu4{sJe)OSyC;KfUJ}k7r&M$05P^`twWv%OT{q75~FU@(PdX5Cjoal7@0h zEqA2TFwT%OF_W{#=7$b@e^{mk3uHpx-Jj1!fjiDXT%2eXZJw zm0dl~Q^a)h5|iPeWzlK4sy~lODpWNz(Du7p+Y~RxXAjk>*_Nvn=)JX3?f5*s1}Rl~ z(3So5egnpoD>xiqq@@tG{pwNXkvv-_lY1pgbEDB+!jl~@G?>1VgVD@h1}N2_D=^wx zu04dwFN(HopeS(vcOQ?>-=Cg+;4C{o<$>v-G6inLBb>rE+r8%QTRSTEvxm`s5FOio z7XD;M`}vWoZa|+XDM06Mta+E#yc0X$C49O7y{Rk6n_IL>t)c33G%(-Pur+(#?c^Uc YN(>SXt^ejJLPR46kDEA=') +API.add_resource(greetings.Hi, '/hi/') +API.add_resource(greetings.Namaste, '/namaste/') +# Resource endpoints for the calculator module +API.add_resource(calculator.Addition, '/addition//') +API.add_resource(calculator.Multiplication, '/multiplication//') +API.add_resource(calculator.Division, '/division//') +API.add_resource(calculator.Substraction, '/substraction//') \ No newline at end of file diff --git a/FlaskWebServiceCloud/my_project/app.py b/FlaskWebServiceCloud/my_project/app.py new file mode 100644 index 0000000..c562162 --- /dev/null +++ b/FlaskWebServiceCloud/my_project/app.py @@ -0,0 +1,24 @@ +from flask import Flask + +import api + +def create_app(): + app = Flask('app', static_folder=None) + + configure_app(app) + register_blueprints(app) + + return app + +def configure_app(app): + app.config.from_object('config') + +def register_blueprints(app): + app.register_blueprint(api.views.BLUEPRINT) + +if __name__ == '__main__': + APP = create_app() + APP.run( + host='0.0.0.0', + debug=True + ) diff --git a/FlaskWebServiceCloud/my_project/configwsgi.py b/FlaskWebServiceCloud/my_project/configwsgi.py new file mode 100644 index 0000000..1eac96c --- /dev/null +++ b/FlaskWebServiceCloud/my_project/configwsgi.py @@ -0,0 +1,17 @@ +from gunicorn.http import wsgi + +class Response(wsgi.Response): + """Class used for altering or forwarding response info such as headers""" + def default_headers(self, *args, **kwargs): + headers = super(Response, self).default_headers(*args, **kwargs) + return [h for h in headers if not h.startswith('Server:')] + +wsgi.Response = Response + +errorlog = '-' +loglevel = 'info' +accesslog = '-' +access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"' +secure_scheme_headers = {'X-FORWARDED-PROTOCOL': 'ssl', + 'X-FORWARDED-PROTO': 'https', + 'X-FORWARDED-SSL': 'on'} diff --git a/FlaskWebServiceCloud/my_project/run.sh b/FlaskWebServiceCloud/my_project/run.sh new file mode 100644 index 0000000..34b77a7 --- /dev/null +++ b/FlaskWebServiceCloud/my_project/run.sh @@ -0,0 +1 @@ +gunicorn -c configwsgi.py --bind 0.0.0.0:5000 wsgi:APP diff --git a/FlaskWebServiceCloud/my_project/wsgi.py b/FlaskWebServiceCloud/my_project/wsgi.py new file mode 100644 index 0000000..7a5cbde --- /dev/null +++ b/FlaskWebServiceCloud/my_project/wsgi.py @@ -0,0 +1,6 @@ +import logging +from app import create_app + +APP = create_app() +GUNICORN_LOGGER = logging.getLogger('gunicorn.error') +APP.logger.handlers = GUNICORN_LOGGER.handlers diff --git a/FlaskWebServiceCloud/requirements.txt b/FlaskWebServiceCloud/requirements.txt new file mode 100644 index 0000000..72f6460 --- /dev/null +++ b/FlaskWebServiceCloud/requirements.txt @@ -0,0 +1,4 @@ +flask +gunicorn +requests +flask_restplus From 64cfd59bdeaa02069242c34cd21457e39dcf77d1 Mon Sep 17 00:00:00 2001 From: Rahul Vaish Date: Sat, 9 Mar 2019 12:41:25 +0530 Subject: [PATCH 2/3] Update README.MD --- README.MD | 61 ++++++++++--------------------------------------------- 1 file changed, 11 insertions(+), 50 deletions(-) diff --git a/README.MD b/README.MD index abd175a..cf62f50 100644 --- a/README.MD +++ b/README.MD @@ -1,52 +1,13 @@ -This repository contains methods to Dockerize Python Projects. +### This implementation aims to Dockerize the Flask Web Service, so that it can be executed on Cloud Environment.
+### Follow the below steps: + - Code the Web Service Project + - Dockerize the Project:
+ ![image](https://user-images.githubusercontent.com/689226/54067237-ca048480-4263-11e9-9770-11b9582c7458.png) + ![image](https://user-images.githubusercontent.com/689226/54067246-ebfe0700-4263-11e9-9226-431f80843b4e.png) + ![image](https://user-images.githubusercontent.com/689226/54067336-c6253200-4264-11e9-80fb-106f3f119bac.png) + ![image](https://user-images.githubusercontent.com/689226/54067358-0a183700-4265-11e9-9bdb-590e52ab444c.png) + - On Azure, Create Resource -> Web -> Web App for Containers
+ ![image](https://user-images.githubusercontent.com/689226/54067506-da6a2e80-4266-11e9-9679-d27b8b6a0578.png) + ![image](https://user-images.githubusercontent.com/689226/54067472-6d569900-4266-11e9-8704-62530b0890e0.png) -### Table of Contents - - HelloWorld Python Docker - - Flask WebService Docker - - Jupyter Notebook on MyBinder - -
-- ###### [HelloWorld Python Docker](https://github.com/rahulvaish/Docker-Python/tree/HelloWorldPythonDocker) - ###### This example illustrates how to Dockerize a simple Python Project. [[Source Code]](https://github.com/rahulvaish/Docker-Python/tree/HelloWorldPythonDocker) - * Download the HelloWorldPythonDocker project. - * Navigate inside the HelloWorldPythonDocker project folder from Docker QuickStart Terminal. - * Execute the below commands- - ![image](https://user-images.githubusercontent.com/689226/50327319-8e83d500-0514-11e9-9f9e-7f04f1680fe9.png) - ![image](https://user-images.githubusercontent.com/689226/50327517-795b7600-0515-11e9-901e-1348699f7682.png) - * Check Docker Hub: [[Docker Hub URL]](https://cloud.docker.com/repository/docker/rahulvaish/helloworldpythondocker) -![image](https://user-images.githubusercontent.com/689226/50343937-ec351300-054e-11e9-9a99-9e3fd3daf5c9.png) - -
- -- ###### [Flask WebService Docker](https://github.com/rahulvaish/Docker-Python/tree/FlaskWebServiceDocker) - ###### This example illustrates how to Dockerize a Flask WebService Project. [[Source Code]](https://github.com/rahulvaish/Docker-Python/tree/FlaskWebServiceDocker) - * Download the FlaskWebServiceDocker project. - * Navigate inside the FlaskWebServiceDocker project folder from Docker QuickStart Terminal. - * Execute the below commands- - ![image](https://user-images.githubusercontent.com/689226/49724797-2d484000-fc90-11e8-94f7-052f4b1a5710.png) - ![image](https://user-images.githubusercontent.com/689226/49724798-2f120380-fc90-11e8-89b5-f6925cb6e74d.png) -
- Before executing the below commands, I preffered to clean the docker history. Followed the below commands:
- - docker rm -vf $(docker ps -a -q)
- - docker rmi -f $(docker images -a -q)
- - ![image](https://user-images.githubusercontent.com/689226/53516344-175d5500-3af2-11e9-9f23-feddbd3135c8.png) - Here we have used -p which specifies the port it is going to run on. In out Calculator.py file we used app.run(debug=True, host=’0.0.0.0') so we needed to specify which port when using flask run , which above you can see I used 5000.
- - ![image](https://user-images.githubusercontent.com/689226/53516510-80dd6380-3af2-11e9-8f07-09f3bf07bc82.png) - * Check Docker Hub: [[Docker Hub URL]](https://hub.docker.com/r/rahulvaish/flaskwebservicedocker/) - ![image](https://user-images.githubusercontent.com/689226/49725174-181fe100-fc91-11e8-8900-84403a4a4b9b.png) - -
- -- ###### [Jupyter Notebook on MyBinder](https://github.com/rahulvaish/Docker-Python/tree/JupyterBinder) - ###### This example illustrates how Jupyter Notebooks can be shared on MyBinder which Dockerizes the Notebook under the hood. [[Source Code]](https://github.com/rahulvaish/Docker-Python/tree/JupyterBinder) - * Open https://mybinder.org/ - * Fill the required fields:
- ![image](https://user-images.githubusercontent.com/689226/50533889-a503df00-0b59-11e9-8c9d-a58b8fbf0c61.png) - Notice:In case you want to share your binder link: https://mybinder.org/v2/gh/rahulvaish/Docker-Python/JupyterBinder?filepath=IRIS.ipynb - * Click Launch
- ![image](https://user-images.githubusercontent.com/689226/50533795-e4313080-0b57-11e9-93a4-583ed0bc576d.png) - * FYI:
- ![image](https://user-images.githubusercontent.com/689226/50533883-6110da00-0b59-11e9-8e83-93327bbdd32a.png) From 51ef780bbecc0d9bc193035200e7585ba51d2adc Mon Sep 17 00:00:00 2001 From: Rahul Vaish Date: Sat, 9 Mar 2019 12:56:59 +0530 Subject: [PATCH 3/3] Update README.MD --- README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.MD b/README.MD index cf62f50..9011954 100644 --- a/README.MD +++ b/README.MD @@ -10,4 +10,4 @@ ![image](https://user-images.githubusercontent.com/689226/54067506-da6a2e80-4266-11e9-9679-d27b8b6a0578.png) ![image](https://user-images.githubusercontent.com/689226/54067472-6d569900-4266-11e9-8704-62530b0890e0.png) - +