Skip to content

Commit

Permalink
Add Docker support
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Slater <[email protected]>
  • Loading branch information
Firefishy committed Dec 13, 2022
1 parent 88358e1 commit bd1bb92
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.git/
*.pyc
.pydevproject
.settings
.project
log/django.osqa.log
tmp/*
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
58 changes: 58 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: ci

on:
push:
branches:
- "**"
tags:
- "v*.*.*"
pull_request:
branches:
- "master"

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
ghcr.io/openstreetmap/osqa
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable={{is_default_branch}}
type=sha
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
52 changes: 52 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# hadolint global ignore=DL3008
FROM debian:buster

RUN apt-get update && apt-get install -y --no-install-recommends \
python \
python-pip \
python-dev \
python-setuptools \
ca-certificates \
gcc \
libpq-dev \
curl \
gunicorn \
&& rm -rf /var/lib/apt/lists/*

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# kelseyhightower confd arm64 builds are broken: https://github.com/kelseyhightower/confd/issues/850
ARG CONFD_VERSION=0.19.1
RUN CONFD_ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) \
&& curl -SL "https://github.com/abtreece/confd/releases/download/v${CONFD_VERSION}/confd-v${CONFD_VERSION}-linux-${CONFD_ARCH}.tar.gz" | tar -xz -C /usr/local/bin/ \
&& /usr/local/bin/confd -version

RUN groupadd --system --gid 510 osqa && useradd --system --gid osqa --uid 510 osqa

WORKDIR /srv/app
RUN chown osqa:osqa /srv/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY --chown=osqa . .

# Add confd templates
COPY docker/confd/ /etc/confd/

# Set Defaults
ENV OSQA_DB_ENGINE=django.db.backends.postgresql_psycopg2
ENV OSQA_DB_NAME=osqa
ENV OSQA_DB_USER=osqa
ENV OSQA_DB_PASSWORD=osqa
ENV OSQA_DB_HOST=postgres
ENV OSQA_ALLOWED_HOSTS=localhost
ENV OSQA_CACHE_BACKEND=memcached://memcached:11211/
ENV OSQA_APP_URL=http://localhost:8080/
ENV OSQA_TIME_ZONE=UTC

ENTRYPOINT ["./docker/entrypoint.sh"]

USER osqa
EXPOSE 8080
CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"]
35 changes: 35 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: "2.1"

services:

osqa:
build:
context: .
environment:
OSQA_DB_USER: osqa
OSQA_DB_PASSWORD: osqa
OSQA_DB_NAME: osqa
OSQA_DB_HOST: postgres

ports:
- 8080:8080
depends_on:
postgres:
condition: service_healthy
memcached:
condition: service_started

postgres:
image: postgres:14
environment:
POSTGRES_USER: osqa
POSTGRES_PASSWORD: osqa
POSTGRES_DB: osqa
healthcheck:
test: ["CMD-SHELL", "pg_isready -U osqa"]
interval: 10s
timeout: 5s
retries: 5

memcached:
image: memcached:1
3 changes: 3 additions & 0 deletions docker/confd/conf.d/settings_local.py.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[template]
src = "settings_local.py.tmpl"
dest = "/srv/app/settings_local.py"
79 changes: 79 additions & 0 deletions docker/confd/templates/settings_local.py.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# encoding:utf-8
import os.path

SITE_SRC_ROOT = os.path.dirname(__file__)
LOGGING = {
'version': 1,
'formatters': {
'default': {
'format': '[OSQA] TIME: %(asctime)s MSG: %(filename)s:%(funcName)s:%(lineno)d %(message)s',
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'default',
},
},
'loggers' : {
# ensure that all log entries are propagated to root
'django': { 'propagate': True },
'django.request': { 'propagate': True },
'django.security': { 'propagate': True },
'py.warnings': { 'propagate': True },
},
'root': {
'handlers': ['console'],
'level': 'DEBUG',
},
}

#ADMINS and MANAGERS
ADMINS = ()
MANAGERS = ADMINS

DEBUG = False
DEBUG_TOOLBAR_CONFIG = {
'INTERCEPT_REDIRECTS': True
}
TEMPLATE_DEBUG = DEBUG
INTERNAL_IPS = ('127.0.0.1',)
ALLOWED_HOSTS = ('{{ getenv "OSQA_ALLOWED_HOSTS" }}',)

DATABASES = {
'default': {
'ENGINE': '{{ getenv "OSQA_DB_ENGINE" }}',
'NAME': '{{ getenv "OSQA_DB_NAME" }}',
'USER': '{{ getenv "OSQA_DB_USER" }}',
'PASSWORD': '{{ getenv "OSQA_DB_PASSWORD" }}',
'HOST': '{{ getenv "OSQA_DB_HOST" }}',
'PORT': '',
'CONN_MAX_AGE': 600,
}
}

CACHE_BACKEND = '{{ getenv "OSQA_CACHE_BACKEND" }}'
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
# Customize the values below if OSQA is in a subfolder and especially you're planning on
# running multiple Django applications (OSQA or others) on the same domain in different
# subfolders
#SESSION_COOKIE_PATH = '/'
#CSRF_COOKIE_PATH = '/'

# This should be equal to your domain name, plus the web application context.
# This shouldn't be followed by a trailing slash.
# I.e., http://www.yoursite.com or http://www.hostedsite.com/yourhostapp
APP_URL = '{{ getenv "OSQA_APP_URL" }}'

#LOCALIZATIONS
TIME_ZONE = '{{ getenv "OSQA_TIME_ZONE" }}'

#OTHER SETTINGS

USE_I18N = True
LANGUAGE_CODE = 'en'

OSQA_DEFAULT_SKIN = 'default'

DISABLED_MODULES = ['books', 'recaptcha', 'project_badges', 'stats', 'localauth', 'facebookauth', 'oauthauth', 'mysqlfulltext']
12 changes: 12 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -ex

/usr/local/bin/confd -onetime -backend env

# apply database migrations
# python manage.py migrate --noinput

python manage.py syncdb --all --noinput
python manage.py migrate --fake --noinput

exec "$@"

0 comments on commit bd1bb92

Please sign in to comment.