Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:22.18.0-alpine as builder
FROM node:22.18.0-alpine AS builder

WORKDIR /server-build

Expand All @@ -17,6 +17,23 @@ COPY . .

RUN pnpm build

# ---- RECIPES IMAGE ----------------------------------------------------------
FROM node:22.18.0-alpine AS recipes

# needed for simple-git repository checks, even though this isn't a git repository
RUN apk add --no-cache git

WORKDIR /recipes

ENV CI=true
ENV NODE_ENV=development

RUN wget -qO- https://github.com/ferdium/ferdium-recipes/archive/refs/heads/main.tar.gz | tar xz --strip-components=1

RUN npm i -gf "pnpm@$(node -p 'require("./package.json").engines.pnpm')" && pnpm -v
RUN pnpm install
RUN pnpm package

# ---- RUNTIME IMAGE ----------------------------------------------------------
FROM node:22.18.0-alpine

Expand All @@ -34,6 +51,10 @@ RUN apk add --no-cache sqlite-libs curl su-exec python3 make g++ py3-pip git py3
COPY --from=builder /server-build /app
RUN npm i -g @adonisjs/cli

# TODO: Do we need to copy more things over?
COPY --from=recipes /recipes/archives /app/recipes/archives
COPY --from=recipes /recipes/all.json /recipes/featured.json /app/recipes/

HEALTHCHECK --start-period=5s --interval=30s --retries=5 --timeout=3s CMD curl -sSf http://localhost:${PORT}/health

COPY docker/entrypoint.sh /entrypoint.sh
Expand Down
74 changes: 22 additions & 52 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -euo pipefail

cat << "EOL"
-------------------------------------
______ ___
Expand All @@ -16,58 +18,31 @@ cat << "EOL"
Brought to you by ferdium.org
EOL

# Update recipes from official git repository

# We need to set NODE_ENV to development to install devDependencies
SAVE_NODE_ENV=$NODE_ENV
NODE_ENV=development

if [ ! -d "/app/recipes/.git" ]; # When we mount an existing volume (ferdium-recipes-vol:/app/recipes) if this is only /app/recipes it is always true
then
echo '**** Generating recipes for first run ****'
git clone --depth 1 --branch main https://github.com/ferdium/ferdium-recipes recipes
else
echo '**** Updating recipes ****'
chown -R root /app/recipes # Fixes ownership problem when doing git pull -r
cd recipes
git stash -u
git pull -r
git stash pop
cd ..
fi

cd recipes
git config --global --add safe.directory /app/recipes
EXPECTED_PNPM_VERSION=$(node -p 'require("./package.json").engines.pnpm')
npm i -gf pnpm@$EXPECTED_PNPM_VERSION
pnpm i
pnpm package
cd ..

# Restore NODE_ENV
NODE_ENV=$SAVE_NODE_ENV
NODE_ENV="${NODE_ENV:-production}"

key_file="${DATA_DIR}/FERDIUM_APP_KEY.txt"
DATA_DIR="${DATA_DIR:-/data}"

print_app_key_message() {
app_key=$1
printf '**** App key is %s. ' ${app_key}
printf 'You can modify `%s` to update the app key ****\n' ${key_file}
}
APP_KEY="${APP_KEY:-}"
JWT_USE_PEM="${JWT_USE_PEM:-false}"

# Create APP key if needed
if [ -z ${APP_KEY} ] && [ ! -f ${key_file} ]
then
echo '**** Generating Ferdium-server app key for first run ****'
adonis key:generate --force
APP_KEY=$(grep APP_KEY .env | cut -d '=' -f2)
echo ${APP_KEY} > ${key_file}
print_app_key_message ${APP_KEY}
if [ ! -z "${APP_KEY}" ]; then
echo "**** APP_KEY env var is set, using it ****"
echo "**** App key is ${APP_KEY} ****"
else
APP_KEY=$(cat ${key_file})
print_app_key_message ${APP_KEY}
KEY_FILE="${DATA_DIR}/FERDIUM_APP_KEY.txt"
if [ -f ${KEY_FILE} ]; then
APP_KEY=$(cat ${KEY_FILE})
else
echo "**** Generating Ferdium-server app key for first run ****"
APP_KEY=$(CACHE_DIR=/tmp/adonis-cache node ace generate:key)
echo ${APP_KEY} > ${KEY_FILE}
fi
echo -n "**** App key is ${APP_KEY}. "
echo "You can modify '${KEY_FILE}' to update the app key ****"
fi


# -------------------------------------
# Create JWT public/private keys if needed

Expand Down Expand Up @@ -100,16 +75,11 @@ fi
# -------------------------------------

export APP_KEY

# Enable the errexit option
set -e
export NODE_ENV

# Run the script to migrate from AdonisJS v4 to v5
sh /app/scripts/adonisjs-4-to-5.sh

# Disable the errexit option
set +e

node ace migration:run --force
CACHE_DIR=/tmp/adonis-cache node ace migration:run --force

node build/server.js