Browse Source

Add container version; add hook for static content

Basti Tee 5 years ago
parent
commit
a46ca9f40d
7 changed files with 76 additions and 4 deletions
  1. 32 0
      Dockerfile
  2. 3 3
      acme/api.py
  3. BIN
      acme/static/favicon.png
  4. 18 0
      docker/nginx.conf
  5. 3 0
      docker/run.sh
  6. 7 0
      docker/uwsgi.ini
  7. 13 1
      make

+ 32 - 0
Dockerfile

@@ -0,0 +1,32 @@
+FROM nginx:stable-alpine
+
+# Default environment
+ENV LANG=en_US.UTF-8
+ENV LC_ALL=en_US.UTF-8
+
+# Install necessary third-party software
+RUN apk update && apk add \
+    vim alpine-sdk linux-headers python3 python3-dev pcre pcre-dev
+RUN pip3 install --upgrade pip uwsgi pipenv
+
+# Install application dependencies
+COPY Pipfile /Pipfile
+RUN pipenv lock -r > /requirements.txt
+RUN pip3 install -r requirements.txt
+
+# Configure NGINX
+RUN echo "daemon off;" >> /etc/nginx/nginx.conf
+COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
+
+# Copy application source code
+COPY acme /app
+COPY docker/uwsgi.ini uwsgi.ini
+
+# Prepare run procedure
+COPY docker/run.sh /run.sh
+RUN chmod a+x /run.sh
+RUN adduser -D -u 1000 uwsgi
+
+# Startup hook
+EXPOSE 80
+ENTRYPOINT [ "/run.sh" ]

+ 3 - 3
acme/api.py

@@ -5,16 +5,16 @@
 from flask import Flask, request
 from .state import AppState
 
-flask_app = Flask(__name__)
+app = Flask(__name__)
 app_state = AppState()
 
 
-@flask_app.route("/", methods=['GET'])
+@app.route("/", methods=['GET'])
 def get_message():  # pylint: disable=E0211
     return app_state.get_message() + '\n'
 
 
-@flask_app.route("/", methods=['POST'])
+@app.route("/", methods=['POST'])
 def set_message():  # pylint: disable=E0211
     message = request.args.get('message')
     app_state.set_message(message)

BIN
acme/static/favicon.png


+ 18 - 0
docker/nginx.conf

@@ -0,0 +1,18 @@
+server {
+    listen 80;
+    server_name  localhost;
+
+    location / {
+        try_files /app @app;
+    }
+    location @app {
+        include uwsgi_params;
+        uwsgi_pass 127.0.0.1:3031;
+    }
+    location /static {
+        alias /app/static;
+    }
+    location /favicon.ico {
+        alias /app/static/favicon.png;
+    }
+}

+ 3 - 0
docker/run.sh

@@ -0,0 +1,3 @@
+#!/bin/sh
+uwsgi uwsgi.ini &
+nginx

+ 7 - 0
docker/uwsgi.ini

@@ -0,0 +1,7 @@
+[uwsgi]
+module = app.api
+callable = app
+socket = 127.0.0.1:3031
+master = 1
+uid = uwsgi
+enable-threads = 1

+ 13 - 1
make

@@ -1,8 +1,11 @@
 #!/bin/bash
 cd "$( cd "$( dirname "$0" )"; pwd )"
 
+TARGET_PORT=9690
 PROJECT_NAME="acme"
-export PIPENV_VERBOSITY=-1  # suppress warning if pipenv is starting inside venv
+IMAGE_TAG="$PROJECT_NAME/$PROJECT_NAME:latest"
+
+export PIPENV_VERBOSITY=-1  # suppress warning if pipenv is started inside venv
 export PYTHONPATH=.
 
 function init {
@@ -39,6 +42,15 @@ function clean {
 	rm -fr build dist .egg $PROJECT_NAME.egg-info .pytest_cache
 }
 
+function dockerbuild {
+    docker build -t "$IMAGE_TAG" . || exit 1
+}
+
+function dockerrun {
+    dockerbuild
+    docker run --rm -it -p $TARGET_PORT:80 --name acme-nginx "$IMAGE_TAG"
+}
+
 function all {
     clean
     init