Skip to content

Commit v0.2

kwmccabe edited this page Apr 17, 2018 · 18 revisions

v0.2 - start.sh, stop.sh, entrypoint.sh, gunicorn-access.log, gunicorn-errors.log, volumes, .gitignore


Files changed (10)

File .gitignore ADDED

  • Exclude temporary files from code repository.
+__pycache__
+*.py[cod]
+*.log
+.DS_Store

File README.md MODIFIED

A sample, fullstack, Python web application
 
-1. Python
-2. Flask
-3. MySQL
-4. Gunicorn
-5. JQuery
-6. Bootstrap
+1. Docker
+2. Python
+3. Flask
+4. MySQL
+5. Gunicorn
+6. JQuery
+7. Bootstrap

File bin/start.sh ADDED

  • Utility script to run docker-compose up and launch services.
  • You should update the BASEDIR value to reflect you local path.
+#!/bin/bash
+
+BASEDIR=/Library/WebServer/docker-hub/flaskapp
+docker-compose -f ${BASEDIR}/docker-compose.yml up --build -d

File bin/stop.sh ADDED

  • Utility script to run docker-compose down and stop associated services.
  • You should update the BASEDIR value to reflect you local path.
+#!/bin/bash
+
+BASEDIR=/Library/WebServer/docker-hub/flaskapp
+docker-compose -f ${BASEDIR}/docker-compose.yml down

File docker-compose.yml MODIFIED

  • Refine the startup process, and link to the container volume so code changes are seen immediately.
  • command : Replace the direct call to gunicorn with the entrypoint.sh script.
  • volumes : Link the local directory ./web with the containers directory /web.
         build: ./web
         ports:
             - "5000:8000"
-        command: /usr/local/bin/gunicorn -b :8000 flaskapp:app
+        command: ./entrypoint.sh
+        volumes:
+            - ./web:/web

File web/.dockerignore ADDED

  • Exclude temporary files when copying code into the container.
+__pycache__
+*.py[cod]
+*.log
+.DS_Store

File web/Dockerfile MODIFIED

  • Separate the installation of requirements.txt. As the project grows, changes to this file become infrequent. If requirements.txt is unchanged between Docker builds, the pip install commands can be skipped.
 RUN mkdir -p /web
 WORKDIR /web
 
-# copy code from current directory into working directory
-COPY . /web
+# copy requirements.txt for pip install
+COPY requirements.txt /web/requirements.txt
 
 # setup python environment, import required modules
 RUN pip install --upgrade pip
 RUN pip install --no-cache-dir -r requirements.txt
 
+# copy code from current directory into working directory
+COPY . /web

File web/entrypoint.sh ADDED

  • Shell script collects Gunicorn - WSGI server launch options in one place.
  • flaskapp:app : Application module MODULE_NAME:VARIABLE_NAME.
  • bind : Application available on this port inside container.
  • workers : The number of worker processes for handling requests.
  • timeout : Extend worker lifetime 30 to allow debugging session.
  • access-logfile : Request log filepath.
  • error-logfile : Error log filepath.
  • log-level : Error log granularity : debug | info | warning | error | critical.
  • reload : Restart workers when code changes.
+#!/bin/sh
+
+# see http://docs.gunicorn.org/en/stable/settings.html
+
+/usr/local/bin/gunicorn \
+    flaskapp:app \
+    --bind :8000 \
+    --workers 2 \
+    --timeout 3600 \
+    --access-logfile logs/gunicorn-access.log \
+    --error-logfile logs/gunicorn-error.log \
+    --log-level info \
+    --reload

File web/flaskapp.py MODIFIED

  • Add a second route /info/date that returns a dynamic value.
+@app.route('/info/date')
+def info_date():
+    import datetime
+    ts = datetime.datetime.now().strftime("%Y/%m/%d @ %H:%M:%S")
+    return "Current Datetime : %s" % ts

File web/requirements.txt MODIFIED

  • Add specific version numbers to required packages, to control changes and allow for testing after upgrades.
-Flask
-gunicorn
+Flask==0.12.2
+gunicorn==19.7.1

Commit-v0.1 | Commit-v0.2 | Commit-v0.3

Clone this wiki locally