Skip to content

Instantly share code, notes, and snippets.

@jerryan999
Last active July 20, 2022 16:20
Show Gist options
  • Save jerryan999/a17b39d5d18df8bd6833dec0a85a5eeb to your computer and use it in GitHub Desktop.
Save jerryan999/a17b39d5d18df8bd6833dec0a85a5eeb to your computer and use it in GitHub Desktop.
python-application-docker
from flask import Flask, request
from pymongo import MongoClient
import redis
import config
import tasks
app = Flask(__name__)
mongo_client = MongoClient(config.MONGO_HOST, config.MONGO_PORT)
redis_client = redis.Redis(host=config.REDIS_HOST, port=config.REDIS_PORT)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
@app.route("/mongo_health")
def mongo_health():
db = mongo_client.test
ok = db.command("ping").get("ok")
return "<p>MongoDB health check: {}</p>".format(ok)
@app.route("/redis_health")
def redis_health():
ok = redis_client.ping()
return "<p>Redis health check: {}</p>".format(ok)
@app.route('/send_mail', methods=['GET'])
def send_mail():
email = request.args.get('email')
tasks.send_mail_async.apply_async(args=[email], countdown=3)
return "<p>Email sent to {}</p>".format(email)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=config.APP_SERVER_PORT)
APP_NAME="app"
APP_SERVER_PORT = 5008
MONGO_HOST = "localhost"
MONGO_PORT = 27017
REDIS_HOST = "localhost"
REDIS_PORT = 6379
CELERY_BROKER_URL = "redis://localhost:6379/0"
CELERY_RESULT_BACKEDN = "redis://localhost:6379/0"
FROM python:3.9.13-slim as python-deps
RUN apt-get update -y && apt-get install -y gcc
WORKDIR /usr/app
RUN python -m venv /usr/app/venv
ENV PATH="/usr/app/venv/bin:$PATH"
COPY requirements.txt ./
RUN pip install --upgrade pip==20.1.1 && \
pip install -r requirements.txt && \
rm requirements.txt
FROM python:3.9.13-slim
RUN apt-get update -y && apt-get install -y supervisor
RUN groupadd -g 999 python && \
useradd -r -u 999 -m -g python ec2-user
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV HOME=/home/ec2-user
ENV APP_HOME=/home/ec2-user/python-app
WORKDIR $APP_HOME
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY . $APP_HOME
COPY --chown=ec2-user:python --from=python-deps /usr/app/venv /usr/app/venv
RUN ln -s /usr/app/venv /home/ec2-user/python-app/venv
RUN chown -R ec2-user:python $APP_HOME
ENV PATH="$APP_HOME/venv/bin:$PATH"
USER 999
ENTRYPOINT ["/home/ec2-user/python-app/start_server.sh"]
#!/bin/bash
# prepare log files and
touch logs/gunicorn-access.log
touch logs/gunicorn.log
# run celery worker in background
supervisord -c /etc/supervisor/conf.d/supervisord.conf
gunicorn -b 0.0.0.0:5008 \
--timeout 300 \
--access-logfile logs/gunicorn-access.log \
--error-logfile logs/gunicorn.log \
--capture-output \
--access-logformat '%(t)s %(p)s %(h)s "%r" %(s)s %(L)s %(b)s "%{Referer}i" "%{User-agent}i"' \
app:app \
exec "$@"
from celery import Celery
import config
celery_client = Celery(config.APP_NAME +"-worker", broker=config.CELERY_BROKER_URL)
@celery_client.task
def send_mail_async(data):
print("Sending mail to {}".format(data))
return "Mail sent to {}".format(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment