Skip to content

Commit

Permalink
feat: dockerize the app (#24)
Browse files Browse the repository at this point in the history
* feat: dockerize the app

* Update .github/workflows/docker.yml

Co-authored-by: Marek Grzelak <[email protected]>

* Address review comments

* Address review comments

---------

Co-authored-by: Marek Grzelak <[email protected]>
  • Loading branch information
m4tx and seqre authored Oct 8, 2023
1 parent 2cc9674 commit 48a711b
Show file tree
Hide file tree
Showing 19 changed files with 336 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile
db.sqlite3
docker-compose.dev.yml
docker-compose.yml
nginx/
11 changes: 11 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DEBUG=1
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=kcc3
SQL_USER=kcc3
SQL_PASSWORD=kcc3
SQL_HOST=db
SQL_PORT=5432
POSTGRES_USER=kcc3
POSTGRES_PASSWORD=kcc3
POSTGRES_DB=kcc3
CELERY_BROKER=amqp://guest:guest@rabbitmq:5672//
12 changes: 12 additions & 0 deletions .env.prod.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DEBUG=0
SECRET_KEY=CHANGE_ME
DJANGO_ALLOWED_HOSTS=fanpai.chombo.club,yakuman.chombo.club
SQL_DATABASE=kcc3
SQL_USER=kcc3
SQL_PASSWORD=CHANGE_ME
SQL_HOST=db
SQL_PORT=5432
POSTGRES_USER=kcc3
POSTGRES_PASSWORD=CHANGE_ME
POSTGRES_DB=kcc3
CELERY_BROKER=amqp://guest:guest@rabbitmq:5672//
71 changes: 71 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Docker Images

on:
push:

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-web:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/riichi/kcc3-web

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

build-and-push-proxy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/riichi/kcc3-proxy

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: nginx/
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ coverage.xml

# Django stuff:
*.log
local_settings.py
db.sqlite3
celerybeat.pid

Expand Down
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.11-slim-bookworm

RUN mkdir -p /app/web
WORKDIR /app/web

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y netcat-openbsd zlib1g-dev libjpeg-dev gcc

RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

COPY . .

ENV MEDIA_ROOT /app/media
ENV STATIC_ROOT /app/static

EXPOSE 8000
ENTRYPOINT ["/app/web/entrypoint.sh"]
CMD ["gunicorn", "kcc3.wsgi:application", "--bind", "0.0.0.0:8000"]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Mateusz Maćkowski
Copyright (c) 2019-2023 Kraków Chombo Club

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# kcc3
[![Docker Images Build Status](https://github.com/riichi/kcc3/workflows/Docker%20Images/badge.svg)](https://github.com/riichi/kcc3/actions/workflows/docker.yml)
[![License](https://shields.io/github/license/riichi/kcc3)](https://github.com/riichi/kcc3/blob/master/LICENSE)

KCC3 is a simple badge server developed for
[Kraków Chombo Club](https://chombo.club/). It puts the focus on providing
simple interface, automate as much as possible and provide features especially
Expand All @@ -19,7 +22,6 @@ useful for Rīchi Mahjong players.
## Quickstart
```
pip install -r requirements.txt
cp kccbadgeserver/settings/{development_settings.py.example,local_settings.py}
python manage.py migrate
python manage.py runserver
```
Expand Down
1 change: 0 additions & 1 deletion badges/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def get_fieldsets(self, request, obj=None):
'fields': general_fields
}),
('Endpoint', {
'classes': ('collapse',),
'fields': ('endpoint_url', 'refresh_interval', 'token'),
}),
)
Expand Down
64 changes: 64 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
x-env:
&env-file
- ./.env.dev

services:
web:
build:
context: .
container_name: kcc3_web
volumes:
- static_volume:/app/static
- media_volume:/app/media
depends_on:
- db
- beat
env_file: *env-file

worker:
build:
context: .
container_name: kcc3_worker
command: celery -A kcc3 worker --loglevel=info
depends_on:
- db
- rabbitmq
env_file: *env-file

beat:
build:
context: .
container_name: kcc3_beat
command: celery -A kcc3 beat -S django --loglevel=info
depends_on:
- db
- rabbitmq
- worker
env_file: *env-file

db:
image: postgres:16
container_name: kcc3_db
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file: *env-file

rabbitmq:
image: rabbitmq:3
container_name: kcc3_rabbitmq

proxy:
build: ./nginx
container_name: kcc3_proxy
volumes:
- static_volume:/app/static
- media_volume:/app/media
ports:
- 8000:80
depends_on:
- web

volumes:
postgres_data:
static_volume:
media_volume:
56 changes: 56 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
x-env:
&env-file
- ./.env.prod

services:
web:
image: ghcr.io/riichi/kcc3-web:master
container_name: kcc3_web
volumes:
- ./static_files:/app/static
- ./media_files:/app/media
depends_on:
- db
- beat
env_file: *env-file

worker:
image: ghcr.io/riichi/kcc3-web:master
container_name: kcc3_worker
command: celery -A kcc3 worker --loglevel=info
depends_on:
- db
- rabbitmq
env_file: *env-file

beat:
image: ghcr.io/riichi/kcc3-web:master
container_name: kcc3_beat
command: celery -A kcc3 beat -S django --loglevel=info
depends_on:
- db
- rabbitmq
- worker
env_file: *env-file

db:
image: postgres:16
container_name: kcc3_db
volumes:
- ./pgdata:/var/lib/postgresql/data/
env_file: *env-file

rabbitmq:
image: rabbitmq:3
container_name: kcc3_rabbitmq

proxy:
image: ghcr.io/riichi/kcc3-proxy:master
container_name: kcc3_proxy
volumes:
- ./static_files:/app/static
- ./media_files:/app/media
ports:
- 8000:80
depends_on:
- web
19 changes: 19 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -e

if [ "$SQL_ENGINE" = "django.db.backends.postgresql" ]
then
echo "Waiting for postgres..."

while ! nc -z "$SQL_HOST" "$SQL_PORT"; do
sleep 0.1
done

echo "PostgreSQL started"
fi

python manage.py migrate --noinput
python manage.py collectstatic --no-input --clear

exec "$@"
2 changes: 1 addition & 1 deletion kcc3/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'kcc3.settings.local_settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'kcc3.settings.settings')

app = Celery('kcc3')

Expand Down
31 changes: 0 additions & 31 deletions kcc3/settings/development_settings.py.example

This file was deleted.

40 changes: 40 additions & 0 deletions kcc3/settings/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from .base import *


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'CHANGE_ME')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(os.environ.get('DEBUG', default=0))

ALLOWED_HOSTS = os.environ.get(
'DJANGO_ALLOWED_HOSTS',
'fanpai.localhost,yakuman.localhost,localhost'
).split(',')
PARENT_HOST = os.environ.get('DJANGO_PARENT_HOST', 'localhost:8000')


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': os.environ.get('SQL_ENGINE', 'django.db.backends.sqlite3'),
'NAME': os.environ.get('SQL_DATABASE', os.path.join(BASE_DIR, 'db.sqlite3')),
'USER': os.environ.get('SQL_USER', None),
'PASSWORD': os.environ.get('SQL_PASSWORD', None),
'HOST': os.environ.get('SQL_HOST', None),
'PORT': os.environ.get('SQL_PORT', None),
}
}


MEDIA_ROOT = os.environ.get('MEDIA_ROOT', os.path.join(BASE_DIR, 'media'))
MEDIA_URL = '/media/'
STATIC_ROOT = os.environ.get('STATIC_ROOT', os.path.join(BASE_DIR, 'static'))

CELERY_BROKER_URL = os.environ.get(
'CELERY_BROKER', 'amqp://guest:guest@localhost:5672//')
Loading

0 comments on commit 48a711b

Please sign in to comment.