restructure for docker

This commit is contained in:
Ville Rantanen
2023-07-24 20:02:45 +03:00
parent 79780f0769
commit 58abf04d2c
45 changed files with 152 additions and 17 deletions

View File

@@ -1,17 +0,0 @@
PYTHON = python3
FLASK = flask
SQLITE = sqlite3
default: venv
test: venv
test/all.sh
venv:
$(PYTHON) -m venv $@
. ./venv/bin/activate && pip3 install -r requirements.txt
forum.db:
$(SQLITE) $@ < schema.txt
.PHONY: test

18
docker-compose.yml Normal file
View File

@@ -0,0 +1,18 @@
version: "3.7"
services:
forum:
build:
context: ./forum/
dockerfile: ./Dockerfile
args:
UID: ${UID}
GID: ${GID}
TZ: ${TZ}
volumes:
- "./forum/:/app"
ports:
- "${EXPOSE}:5000"
environment:
ADMINU: ${ADMINU}
ADMINP: ${ADMINP}

7
example.env Normal file
View File

@@ -0,0 +1,7 @@
UID=1000
GID=1000
TZ=Europe/Helsinki
ADMINU=admin
ADMINP=admin
EXPOSE=5000

36
forum/Dockerfile Normal file
View File

@@ -0,0 +1,36 @@
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -yqq \
&& apt-get install -y --no-install-recommends \
sqlite3 \
tzdata \
git \
make \
python3-venv \
python3-pip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
ARG UID
ARG GID
ARG TZ
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN groupadd -g $GID user && \
useradd -u $UID -g $GID -ms /bin/bash user && \
mkdir -p /opt/venv && chown $UID:$GID /opt/venv
COPY ./requirements.txt /requirements.txt
COPY docker-builder.sh /
USER user
RUN bash /docker-builder.sh
#COPY ./ /app
#USER root
#RUN chown -R $UID:$GID /app
USER user
WORKDIR /app
COPY docker-entrypoint.sh /
CMD bash /docker-entrypoint.sh

7
forum/docker-builder.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/sh
set -eux
python3 -m venv /opt/venv
. /opt/venv/bin/activate
pip3 install -r requirements.txt

50
forum/docker-entrypoint.sh Executable file
View File

@@ -0,0 +1,50 @@
#!/bin/sh
PYTHON=python3
SQLITE=sqlite3
export DB="/app/forum.db"
export SERVER=gunicorn
export PID="forum.pid"
export WORKERS=4
if [[ $( stat -c %u /app ) -ne $( id -u ) ]]; then
echo User id and /app folder owner do not match
printf 'UID: %s\nFolder: %s\n' $( id -u ) $( stat -c %u /app )
exit 1
fi
set -eu
. /opt/venv/bin/activate
if [[ -e "$DB" ]]; then
$SQLITE -header "$DB" "SELECT version,name,description,registration_enabled,login_required FROM config"
echo Database already exists
else
password=$($PYTHON tool.py password "$ADMINP")
time=$($PYTHON -c 'import time; print(time.time_ns())')
version=$($PYTHON tool.py version)
$SQLITE "$DB" -init schema.txt "insert into config (
version,
name,
description,
secret_key,
captcha_key,
registration_enabled,
login_required
)
values (
'$version',
'Forum',
'',
'$(head -c 30 /dev/urandom | base64)',
'$(head -c 30 /dev/urandom | base64)',
0,
1
)"
$SQLITE "$DB" "
insert into users (name, password, role, join_time)
values (lower('$ADMINU'), '$password', 2, $time)
"
fi
exec "$SERVER" -w $WORKERS 'main:app' --pid="$PID" -b 0.0.0.0:5000

27
forum/restart.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/usr/bin/env bash
set -e
SERVER="$1"
if [ -z "$SERVER" ]
then
echo "SERVER is not set" >&2
exit 1
fi
case "$SERVER" in
dev)
touch main.py
;;
gunicorn)
if [ -z "$PID" ]
then
echo "PID is not set" >&2
exit 1
fi
kill -hup $(cat "$PID")
;;
*)
echo "Unsupported $SERVER" >&2
exit 1
;;
esac

BIN
forum/static/button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

3
forum/version.py Executable file
View File

@@ -0,0 +1,3 @@
VERSION = "agreper-v0.1.1q1"

4
stop.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -e
docker-compose down forum