From f72461409a3ccbfef13b07b7abdbab214efd78cc Mon Sep 17 00:00:00 2001 From: Vincent De Smet Date: Sun, 12 Mar 2017 05:08:49 +0800 Subject: [PATCH] Add alpine based docker image --- .dockerignore | 5 ++++ Dockerfile | 48 +++++++++++++++++++++++++++++++++++++ bin/uwsgi-start.sh | 21 ++++++++++++++++ docker-compose.yml | 30 +++++++++++++++++++++++ hc/local_settings.py.docker | 42 ++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 bin/uwsgi-start.sh create mode 100644 docker-compose.yml create mode 100644 hc/local_settings.py.docker diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..1d8d0c9a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +*.md +*.yml +.git/ +.gitignore +Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..946199e6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +FROM python:3-alpine + +# Caching layer with bash, tini, UWSGI and mysql / pg clients +RUN set -ex \ + && apk add --no-cache mariadb-libs mariadb-client-libs postgresql-libs tini bash \ + && apk add --no-cache --virtual .build-deps \ + gcc \ + # uwsgi needs linux headers + linux-headers \ + # pip install needs py3 & musl dev + python3-dev \ + musl-dev \ + mariadb-dev \ + postgresql-dev\ + && pip3 install --no-cache-dir uwsgi mysqlclient psycopg2==2.6.2 \ + && apk del .build-deps + +# Add hc user +RUN adduser -D -u 1000 hc + +# Install app requirements +COPY requirements.txt /usr/src/app/ +WORKDIR /usr/src/app/ +RUN set -ex \ + && apk add --no-cache --virtual .build-deps \ + gcc \ + python3-dev \ + musl-dev \ + && pip3 install braintree \ + && pip3 install --no-cache-dir -r requirements.txt \ + && apk del .build-deps + + +# Copy application source +COPY . /usr/src/app + +# Read settings from env vars +RUN cp hc/local_settings.py.docker hc/local_settings.py + +# Pre-compile assets +RUN set -ex \ + && ./manage.py collectstatic --no-input \ + && ./manage.py compress \ + && chown -R hc:hc /usr/src/app + +EXPOSE 9090 +ENTRYPOINT [ "/usr/bin/tini", "--" ] +CMD [ "/usr/src/app/bin/uwsgi-start.sh" ] diff --git a/bin/uwsgi-start.sh b/bin/uwsgi-start.sh new file mode 100755 index 00000000..4c33430d --- /dev/null +++ b/bin/uwsgi-start.sh @@ -0,0 +1,21 @@ +#!/bin/sh +set -ex + +# wait for database connection to run migration +while ! ./manage.py migrate 2>&1; do + sleep 5 +done + +#replace bash with uwsgi +exec uwsgi --master \ + --uid hc \ + --gid hc \ + --http-socket 0.0.0.0:9090 \ + --processes 2 \ + --threads 2 \ + --chdir /usr/src/app \ + --module hc.wsgi:application \ + --enable-threads \ + --thunder-lock \ + --static-map /static=/usr/src/app/static-collected \ + --attach-daemon "./manage.py sendalerts" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..fdd444ab --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +version: '2' +services: + hc: + build: . + # image: haswalt/docker-healthchecks + image: quay.io/honestbee/healthcheck:latest + ports: + - "9090:9090" + depends_on: + - db + environment: + HEALTHCHECKS_EMAIL_HOST: mailhog + HEALTHCHECKS_EMAIL_PORT: 1025 + HEALTHCHECKS_DB: mysql + HEALTHCHECKS_DB_HOST: db + HEALTHCHECKS_DB_USER: root + HEALTHCHECKS_DB_PASSWORD: pa55word + HEALTHCHECKS_SITE_ROOT: http://localhost:9090 + HEALTHCHECKS_DEBUG: 1 + mailhog: + image: mailhog/mailhog + ports: + - "8025:8025" + db: + image: mariadb + environment: + MYSQL_ROOT_PASSWORD: pa55word + MYSQL_USER: hc + MYSQL_PASSWORD: pa55word + MYSQL_DATABASE: hc diff --git a/hc/local_settings.py.docker b/hc/local_settings.py.docker new file mode 100644 index 00000000..e3993da9 --- /dev/null +++ b/hc/local_settings.py.docker @@ -0,0 +1,42 @@ +""" +Local settings for the HealthChecks app +""" + +import os + +ALLOWED_HOSTS = ['*'] +DEBUG = os.getenv('HEALTHCHECKS_DEBUG', False) + +HOST = os.getenv('HEALTHCHECKS_HOST', "localhost") +SITE_ROOT = os.getenv('HEALTHCHECKS_SITE_ROOT', "http://localhost:9090") +PING_ENDPOINT = SITE_ROOT + "/ping/" + +DEFAULT_FROM_EMAIL = os.getenv('HEALTHCHECKS_EMAIL_FROM', "healthchecks@example.org") +EMAIL_HOST = os.getenv('HEALTHCHECKS_EMAIL_HOST', "localhost") +EMAIL_PORT = os.getenv('HEALTHCHECKS_EMAIL_PORT', 25) +EMAIL_HOST_USER = os.getenv('HEALTHCHECKS_EMAIL_USER', "") +EMAIL_HOST_PASSWORD = os.getenv('HEALTHCHECKS_EMAIL_PASSWORD', "") + +if os.environ.get("HEALTHCHECKS_DB") == "postgres": + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': os.getenv("HEALTHCHECKS_DB_NAME", "hc"), + 'USER': os.getenv('HEALTHCHECKS_DB_USER', "postgres"), + 'PASSWORD': os.getenv('HEALTHCHECKS_DB_PASSWORD', ""), + 'HOST': os.getenv('HEALTHCHECKS_DB_HOST', "localhost"), + 'TEST': {'CHARSET': 'UTF8'} + } + } + +if os.environ.get("HEALTHCHECKS_DB") == "mysql": + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'USER': os.getenv('HEALTHCHECKS_DB_USER', "root"), + 'PASSWORD': os.getenv('HEALTHCHECKS_DB_PASSWORD', ""), + 'NAME': os.getenv("HEALTHCHECKS_DB_NAME", "hc"), + 'HOST': os.getenv('HEALTHCHECKS_DB_HOST', "localhost"), + 'TEST': {'CHARSET': 'UTF8'} + } + }