From a70ef27cb2a35869e677f18baa021e96c81388dd Mon Sep 17 00:00:00 2001 From: jamesread Date: Mon, 9 Sep 2024 02:45:41 +0100 Subject: [PATCH] wip: Arch, devcontainer + dist, supervisord --- .devcontainer/devcontainer.json | 14 +++++++ .dockerignore | 6 +++ Dockerfile | 61 +++++++++++++++++------------ package.json | 4 +- var/docker/entrypoint.sh | 36 +++++++++++++++++ var/docker/supervisord/backend.ini | 5 +++ var/docker/supervisord/base.ini | 6 +++ var/docker/supervisord/cron.ini | 5 +++ var/docker/supervisord/frontend.ini | 5 +++ var/docker/supervisord/workers.ini | 5 +++ 10 files changed, 119 insertions(+), 28 deletions(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .dockerignore create mode 100755 var/docker/entrypoint.sh create mode 100644 var/docker/supervisord/backend.ini create mode 100644 var/docker/supervisord/base.ini create mode 100644 var/docker/supervisord/cron.ini create mode 100644 var/docker/supervisord/frontend.ini create mode 100644 var/docker/supervisord/workers.ini diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 000000000..6854df0b7 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,14 @@ +{ + "name": "Postiz Dev Container", + "image": "localhost/postiz-devcontainer", + "features": {}, + "customizations": { + "vscode": { + "settings": {}, + "extensions": [] + } + }, + "forwardPorts": ["4200:4200", "3000:3000"], + "mounts": ["source=/apps,destination=/apps/dist/,type=bind,consistency=cached"] +} + diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..6b6f71eed --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +# We want the docker builds to be clean, and as fast as possible. Don't send +# any half-built stuff in the build context as a pre-caution (also saves copying +# 180k files in node_modules that isn't used!). +node_modules +dist +.nx diff --git a/Dockerfile b/Dockerfile index 5e87f34a3..46619784b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,43 +1,52 @@ -# Foundation image -FROM registry.fedoraproject.org/fedora-minimal:40 AS foundation +# Base image +FROM docker.io/node:20.17-alpine3.19 AS base -RUN microdnf install --nodocs --noplugins --setopt=keepcache=0 --setopt=install_weak_deps=0 -y \ - npm \ - node \ - && microdnf clean all +ENV NPM_CONFIG_UPDATE_NOTIFIER=false +ENV NEXT_TELEMETRY_DISABLED=1 -# Builder image -FROM foundation AS builder +RUN apk add --no-cache \ + bash=5.2.21-r0 \ + supervisor=4.2.5-r4 -RUN mkdir /src +WORKDIR /app -COPY . /src +EXPOSE 4200 +EXPOSE 3000 -WORKDIR /src +RUN mkdir -p /config -RUN npx nx reset -RUN npm run build +COPY .env.example /config/.env -# Output image -FROM foundation AS dist +VOLUME /config LABEL org.opencontainers.image.source=https://github.com/gitroomhq/postiz-app -LABEL org.opencontainers.image.title="Postiz App" -RUN mkdir -p /config /app +# Builder image +FROM base AS devcontainer -VOLUME /config +COPY nx.json tsconfig.base.json package.json package-lock.json /app/ +COPY apps /app/apps/ +COPY libraries /app/libraries/ -COPY --from=builder /src/dist /app/dist/ -COPY --from=builder /src/package.json /app/ -COPY --from=builder /src/nx.json /app/ +RUN npm ci --no-fund && npm run build -COPY .env.example /config/.env -COPY var/docker-entrypoint.sh /app/entrypoint.sh +COPY var/docker/entrypoint.sh /app/entrypoint.sh +COPY var/docker/supervisord/* /app/supervisord_configs/ -EXPOSE 4200 -EXPOSE 3000 +LABEL org.opencontainers.image.title="Postiz App (DevContainer)" -WORKDIR /app +ENTRYPOINT ["/app/entrypoint.sh"] + +# Output image +FROM base AS dist + +COPY --from=devcontainer /app/node_modules/ /app/node_modules/ +COPY --from=devcontainer /app/dist/ /app/dist/ + +COPY package.json nx.json /app/ +COPY var/docker/entrypoint.sh /app/entrypoint.sh + +## Labels at the bottom, because CI will eventially add dates, commit hashes, etc. +LABEL org.opencontainers.image.title="Postiz App (Production)" ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/package.json b/package.json index 2ea2cf88b..efd4a8174 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "prisma-generate": "cd ./libraries/nestjs-libraries/src/database/prisma && npx prisma generate", "prisma-db-push": "cd ./libraries/nestjs-libraries/src/database/prisma && npx prisma db push", "prisma-reset": "cd ./libraries/nestjs-libraries/src/database/prisma && npx prisma db push --force-reset && npx prisma db push", - "docker-build": "docker rmi localhost/postiz || true && docker build -t localhost/postiz . ", - "docker-create": "docker kill postiz || true && docker rm postiz || true && docker create --name postiz -p 3000:3000 -p 4200:4200 localhost/postiz", + "docker-build": "docker rmi localhost/postiz || true && docker build --target dist -t localhost/postiz -f Dockerfile . && docker build --target devcontainer -t localhost/postiz-devcontainer -f Dockerfile .", + "docker-create": "docker kill postiz || true && docker rm postiz || true && docker create --name postiz -p 3000:3000 -p 4200:4200 localhost/postiz", "postinstall": "npm run prisma-generate" }, "private": true, diff --git a/var/docker/entrypoint.sh b/var/docker/entrypoint.sh new file mode 100755 index 000000000..609fa7d04 --- /dev/null +++ b/var/docker/entrypoint.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +if [[ "$SKIP_CONFIG_CHECK" -ne "true" ]]; then + if [ ! -f /config/.env ]; then + echo "ERROR: No .env file found in /config/.env" + fi + + ln -s /config/env /app/.env +fi + +if [[ "$POSTIZ_APPS" -eq "" ]]; then + echo "POSTIZ_APPS is not set, starting everything!" + POSTIZ_APPS="frontend workers cron" +fi + +mkdir -p /etc/supervisor.d/ + +cp /app/supervisord_configs/base.conf /etc/supervisor.d/ + +if [[ "$POSTIZ_APPS" == *"frontend"* ]]; then + cp /app/supervisord_configs/frontend.conf /etc/supervisor.d/ +fi + +if [[ $POSTIZ_APPS == *"workers"* ]]; then + cp /app/supervisord_configs/workers.conf /etc/supervisor.d/ +fi + +if [[ $POSTIZ_APPS == *"cron"* ]]; then + cp /app/supervisord_configs/cron.conf /etc/supervisor.d/ +fi + +if [[ $POSTIZ_APPS == *"backend"* ]]; then + cp /app/supervisord_configs/backend.conf /etc/supervisor.d/ +fi + +/usr/bin/supervisord diff --git a/var/docker/supervisord/backend.ini b/var/docker/supervisord/backend.ini new file mode 100644 index 000000000..8e56645cb --- /dev/null +++ b/var/docker/supervisord/backend.ini @@ -0,0 +1,5 @@ +[program:backend] +directory=/app +command=npm run start:prod +autostart=true +autorestart=false diff --git a/var/docker/supervisord/base.ini b/var/docker/supervisord/base.ini new file mode 100644 index 000000000..b220a9839 --- /dev/null +++ b/var/docker/supervisord/base.ini @@ -0,0 +1,6 @@ +[supervisord] +nodaemon=true +logfile=/dev/null +logfile_maxbytes=0 + + diff --git a/var/docker/supervisord/cron.ini b/var/docker/supervisord/cron.ini new file mode 100644 index 000000000..ef22047a1 --- /dev/null +++ b/var/docker/supervisord/cron.ini @@ -0,0 +1,5 @@ +[program:cron] +directory=/app +command=npm run start:prod:cron +autostart=true +autorestart=false diff --git a/var/docker/supervisord/frontend.ini b/var/docker/supervisord/frontend.ini new file mode 100644 index 000000000..737aedd97 --- /dev/null +++ b/var/docker/supervisord/frontend.ini @@ -0,0 +1,5 @@ +[program:frontend] +directory=/app +command=npm run start:prod:frontend +autostart=true +autorestart=false diff --git a/var/docker/supervisord/workers.ini b/var/docker/supervisord/workers.ini new file mode 100644 index 000000000..fd84a708e --- /dev/null +++ b/var/docker/supervisord/workers.ini @@ -0,0 +1,5 @@ +[program:workers] +directory=/app +command=npm run start:prod:workers +autostart=true +autorestart=false