Skip to content

Commit 1c9240c

Browse files
Creating docker-compose.prod.yml for use in TrueNAS, etc. Reworking Dockerfiles to enable this new functionality. Leaving docker-compose.yml for use in local development.
Running alembic migrations whenever api container is started. Adding DEVELOPMENT.md document to help a developer start with WROLPi.
1 parent 64660d7 commit 1c9240c

File tree

19 files changed

+351
-134
lines changed

19 files changed

+351
-134
lines changed

DEVELOPMENT.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
This document contains instructions to set up your system for WROLPi development.
2+
3+
# Setup
4+
5+
## Debian 12
6+
7+
1. Install accessory packages necessary for testing:
8+
* `sudo apt install ffmpeg chromium chromium-driver sysstat aria2 nodejs npm`
9+
2. Create Python environment:
10+
* `python -m venv venv`
11+
3. Activate the new environment:
12+
* `. venv/bin/activate`
13+
4. Build the docker containers:
14+
* `./scripts/dev_build.sh`
15+
5. Start the docker containers:
16+
* `docker compose up`
17+
6. Browse to the development containers:
18+
* https://localhost:8443
19+
7. Install npm dependencies used outside the app:
20+
21+
8. Run the tests. All tests should pass.
22+
* `pytest`

alembic/env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from logging.config import fileConfig
34

45
from alembic import context

docker-compose.yml

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Development containers, these should only be used if you are developing.
2-
version: '3'
32
services:
43
db: # https://github.com/docker-library/docs/tree/master/postgres
54
image: postgres:12
@@ -15,6 +14,7 @@ services:
1514
- ./pg_data:/var/lib/postgresql/data
1615

1716
api:
17+
image: lrnselfreliance/wrolpi:wrolpi-api-latest
1818
depends_on:
1919
- db
2020
build:
@@ -28,7 +28,6 @@ services:
2828
- './test:/opt/wrolpi/test'
2929
- './test:/media/wrolpi'
3030
- './alembic:/opt/wrolpi/alembic'
31-
- './alembic.ini:/opt/wrolpi/alembic.ini'
3231
ports:
3332
- ${REACT_APP_API-0.0.0.0:8081}:8081
3433
user: '${UID-1000}:${GID-1000}'
@@ -37,6 +36,7 @@ services:
3736
interval: 2m
3837

3938
archive: # Python/Sanic wrapper around Singlefile and Readability.
39+
image: lrnselfreliance/wrolpi:wrolpi-archive-latest
4040
build:
4141
context: .
4242
dockerfile: docker/archive/Dockerfile
@@ -49,6 +49,7 @@ services:
4949
interval: 2m
5050

5151
app: # React App
52+
image: lrnselfreliance/wrolpi:wrolpi-app-latest
5253
depends_on:
5354
- api
5455
build:
@@ -59,7 +60,7 @@ services:
5960
- './app/src:/app/src'
6061
- './app/package.json:/app/package.json'
6162
- '.env:/app/.env'
62-
command: 'npm run start'
63+
command: 'serve -s build -l 3000'
6364
stdin_open: true
6465
healthcheck:
6566
test: [ 'CMD-SHELL', 'curl http://127.0.0.1:3000' ]
@@ -84,24 +85,24 @@ services:
8485
interval: 1m
8586

8687
map_https: # HTTPS for map tile server
88+
image: lrnselfreliance/wrolpi:wrolpi-map_https-latest
8789
depends_on:
8890
- map
89-
environment:
90-
- UPSTREAM_HOST=map
9191
build:
9292
context: .
9393
dockerfile: docker/https_proxy/Dockerfile
94+
args:
95+
- UPSTREAM_HOST=map
9496
ports:
9597
- '8084:443'
96-
volumes:
97-
- './docker/map/nginx.map.conf:/etc/nginx/nginx.conf'
9898
healthcheck:
9999
test: [ 'CMD-SHELL', 'curl http://127.0.0.1:443' ]
100100
interval: 1m
101101

102102
web:
103103
depends_on:
104104
- app
105+
image: lrnselfreliance/wrolpi:wrolpi-web-latest
105106
build:
106107
context: .
107108
dockerfile: docker/web/Dockerfile
@@ -119,6 +120,7 @@ services:
119120
interval: 1m
120121

121122
zim: # kiwix-serve
123+
image: lrnselfreliance/wrolpi:wrolpi-zim-latest
122124
build:
123125
context: .
124126
dockerfile: docker/zim/Dockerfile
@@ -130,22 +132,22 @@ services:
130132
interval: 1m
131133

132134
zim_https: # HTTPS for Zim service.
135+
image: lrnselfreliance/wrolpi:wrolpi-zim_https-latest
133136
depends_on:
134137
- zim
135-
environment:
136-
- UPSTREAM_HOST=zim
137138
build:
138139
context: .
139140
dockerfile: docker/https_proxy/Dockerfile
141+
args:
142+
- UPSTREAM_HOST=zim
140143
ports:
141144
- '8085:443'
142-
volumes:
143-
- './docker/zim/nginx.zim.conf:/etc/nginx/nginx.conf'
144145
healthcheck:
145146
test: [ 'CMD-SHELL', 'curl http://127.0.0.1:443' ]
146147
interval: 1m
147148

148149
help: # The WROLPi Help documentation.
150+
image: lrnselfreliance/wrolpi:wrolpi-help-latest
149151
build:
150152
context: .
151153
dockerfile: docker/help/Dockerfile
@@ -156,17 +158,17 @@ services:
156158
interval: 1m
157159

158160
help_https: # HTTPS for Help service.
161+
image: lrnselfreliance/wrolpi:wrolpi-help_https-latest
159162
depends_on:
160163
- help
161-
environment:
162-
- UPSTREAM_HOST=help
163164
build:
164165
context: .
165166
dockerfile: docker/https_proxy/Dockerfile
167+
args:
168+
- UPSTREAM_HOST=help
169+
- UPSTREAM_PORT=8086
166170
ports:
167171
- '8086:443'
168-
volumes:
169-
- './docker/help/nginx.help.conf:/etc/nginx/nginx.conf'
170172
healthcheck:
171173
test: [ 'CMD-SHELL', 'curl http://127.0.0.1:443' ]
172174
interval: 1m

docker/api/Dockerfile

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@ FROM python:3.11-bookworm
22
ENV DOCKER=true
33
WORKDIR /opt/wrolpi
44

5-
# Update dependencies for the services install.
6-
RUN apt-get update && apt-get install -y ffmpeg catdoc sysstat aria2 iputils-ping
7-
RUN ffmpeg -version
8-
# yt-dlp needs cache directory
5+
# Update dependencies and install required packages
6+
RUN apt-get update && \
7+
apt-get install -y ffmpeg catdoc sysstat aria2 postgresql-client iputils-ping && \
8+
ffmpeg -version && \
9+
apt-get clean && \
10+
rm -rf /var/lib/apt/lists/*
11+
12+
# Create cache directory for yt-dlp
913
RUN mkdir /.cache && chown -R 1000:1000 /.cache
1014

11-
# Install dependencies.
15+
# Install Python dependencies
1216
COPY requirements.txt /opt/wrolpi/requirements.txt
13-
RUN pip3 install -r /opt/wrolpi/requirements.txt
17+
RUN pip3 install --no-cache-dir -r /opt/wrolpi/requirements.txt
1418

15-
# Install WROLPi.
19+
# Copy WROLPi application files
1620
COPY main.py /opt/wrolpi/
1721
COPY wrolpi /opt/wrolpi/wrolpi
1822
COPY modules /opt/wrolpi/modules
23+
COPY alembic /opt/wrolpi/alembic
24+
COPY docker/api/alembic.ini /opt/wrolpi/alembic.ini
25+
26+
# Copy and set up entrypoint script
27+
COPY docker/api/entrypoint.sh /entrypoint.sh
28+
RUN chmod +x /entrypoint.sh
1929

20-
ENTRYPOINT [ "python3", "-OO", "/opt/wrolpi/main.py"]
21-
CMD ["-v", "api", "--host", "0.0.0.0"]
30+
# Use entrypoint script which will run alembic migrations before starting API.
31+
ENTRYPOINT ["/entrypoint.sh"]

docker/api/alembic.ini

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = alembic
6+
7+
# template used to generate migration files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
# sys.path path, will be prepended to sys.path if present.
11+
# defaults to the current working directory.
12+
prepend_sys_path = .
13+
14+
# timezone to use when rendering the date
15+
# within the migration file as well as the filename.
16+
# string value is passed to dateutil.tz.gettz()
17+
# leave blank for localtime
18+
# timezone =
19+
20+
# max length of characters to apply to the
21+
# "slug" field
22+
# truncate_slug_length = 40
23+
24+
# set to 'true' to run the environment during
25+
# the 'revision' command, regardless of autogenerate
26+
# revision_environment = false
27+
28+
# set to 'true' to allow .pyc and .pyo files without
29+
# a source .py file to be detected as revisions in the
30+
# versions/ directory
31+
# sourceless = false
32+
33+
# version location specification; this defaults
34+
# to alembic/versions. When using multiple version
35+
# directories, initial revisions must be specified with --version-path
36+
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
37+
38+
# the output encoding used when revision files
39+
# are written from script.py.mako
40+
# output_encoding = utf-8
41+
42+
sqlalchemy.url = postgresql://postgres:wrolpi@db:5432/wrolpi
43+
44+
45+
[post_write_hooks]
46+
# post_write_hooks defines scripts or Python functions that are run
47+
# on newly generated revision scripts. See the documentation for further
48+
# detail and examples
49+
50+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
51+
# hooks = black
52+
# black.type = console_scripts
53+
# black.entrypoint = black
54+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
55+
56+
# Logging configuration
57+
[loggers]
58+
keys = root,sqlalchemy,alembic
59+
60+
[handlers]
61+
keys = console
62+
63+
[formatters]
64+
keys = generic
65+
66+
[logger_root]
67+
level = WARN
68+
handlers = console
69+
qualname =
70+
71+
[logger_sqlalchemy]
72+
level = WARN
73+
handlers =
74+
qualname = sqlalchemy.engine
75+
76+
[logger_alembic]
77+
level = INFO
78+
handlers =
79+
qualname = alembic
80+
81+
[handler_console]
82+
class = StreamHandler
83+
args = (sys.stderr,)
84+
level = NOTSET
85+
formatter = generic
86+
87+
[formatter_generic]
88+
format = %(levelname)-5.5s [%(name)s] %(message)s
89+
datefmt = %H:%M:%S

docker/api/entrypoint.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Wait for PostgreSQL to be ready
5+
echo "Waiting for PostgreSQL..."
6+
until pg_isready -h db -p 5432 -U postgres; do
7+
echo "PostgreSQL not ready, waiting..."
8+
sleep 2
9+
done
10+
11+
# Run Alembic migrations
12+
echo "Running Alembic migrations..."
13+
python3 -m alembic upgrade head
14+
15+
# Start Sanic service
16+
echo "Starting Sanic service..."
17+
exec python3 -OO /opt/wrolpi/main.py -v api --host 0.0.0.0

docker/app/Dockerfile

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# base image
2-
FROM node:18-bullseye
3-
1+
FROM node:18-alpine
42
WORKDIR /app
5-
ENV PATH /app/node_modules/.bin:$PATH
3+
ENV PATH=/app/node_modules/.bin:$PATH
4+
5+
# Install serve and production dependencies
6+
RUN npm install -g serve && \
7+
npm cache clean --force
68

7-
# install and cache app dependencies
8-
USER root
9-
RUN npm install -g serve
10-
COPY app/public /app/public
11-
COPY app/src /app/src
12-
COPY app/package.json app/package-lock.json /app/
13-
# "node" user will run the app after build.
14-
RUN chown -R node:node /app
9+
# Copy package files and install production dependencies
10+
COPY app/package.json app/package-lock.json ./
11+
RUN npm ci --omit=dev && \
12+
npm cache clean --force
1513

16-
# Install dependencies as "node", this is to avoid changing ownership of many files.
17-
USER node
18-
RUN npm install
14+
# Copy source files and build
15+
COPY app/public ./public
16+
COPY app/src ./src
17+
RUN npm run build
1918

20-
# Run the production build
21-
CMD ["npm", "run", "start"]
19+
# Serve the production build
20+
CMD ["serve", "-s", "build", "-l", "3000"]

docker/archive/Dockerfile

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
FROM browserless/chrome:1.61.1-chrome-stable
2+
WORKDIR /app
3+
ENV PYTHONUNBUFFERED=1
24

5+
# Switch to root for package installation
36
USER root
4-
WORKDIR /app
57

6-
# Install Python for main.py
7-
ENV PYTHONUNBUFFERED=1
8-
RUN apt update && apt install -y python3 python3-dev python3-venv curl && ln -sf python3 /usr/bin/python
8+
# Install Python and dependencies
9+
RUN apt-get update && \
10+
apt-get install -y python3 python3-dev python3-venv curl && \
11+
ln -sf python3 /usr/bin/python && \
12+
apt-get clean && \
13+
rm -rf /var/lib/apt/lists/*
14+
15+
# Set up virtual environment
916
RUN python3 -m venv /app/venv
10-
RUN . /app/venv/bin/activate
1117

12-
# Install a newer singlefile and Readability.
18+
# Install Node.js dependencies
1319
1420

15-
# Install the Python wrapper and it's requirements.
21+
# Install Python dependencies
1622
COPY docker/archive/requirements.txt /app/requirements.txt
17-
RUN /app/venv/bin/pip3 install -r /app/requirements.txt
18-
COPY docker/archive /app
23+
RUN /app/venv/bin/pip3 install --no-cache-dir -r /app/requirements.txt
24+
25+
# Copy application files
26+
COPY docker/archive/main.py /app/
1927

20-
# Launch Archive app in the virtual environment.
21-
ENTRYPOINT [ "/app/venv/bin/python3", "/app/main.py"]
28+
# Launch Archive app in virtual environment
29+
ENTRYPOINT ["/app/venv/bin/python3", "/app/main.py"]

0 commit comments

Comments
 (0)