Merge pull request #205 from jamesread/dockerfile

feat: add dockerfile for production and developer use
This commit is contained in:
Jonathan Irvin 2024-09-09 15:58:10 -05:00 committed by GitHub
commit d93d64460b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 205 additions and 0 deletions

View File

@ -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"]
}

9
.dockerignore Normal file
View File

@ -0,0 +1,9 @@
# 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
.devcontainer
.git
*.md

34
.github/workflows/build-containers.yml vendored Normal file
View File

@ -0,0 +1,34 @@
---
name: "Build Tag"
on:
push:
tags:
- '*'
jobs:
build-tag:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Login to ghcr
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: docker build
run: /var/run/docker-build.sh
- name: docker tag
run: |
docker tag localhost/postiz ghcr.io/githubhq/postiz-app:${{ GITHUB_REF_NAME }}
docker push ghcr.io/githubhq/postiz-app:${{ GITHUB_REF_NAME }}
docker tag localhost/postiz-devcontainer ghcr.io/githubhq/postiz-app:${{ GITHUB_REF_NAME }}
docker push ghcr.io/githubhq/postiz-devcontainer:${{ GITHUB_REF_NAME }}

58
Dockerfile Normal file
View File

@ -0,0 +1,58 @@
# This Dockerfile is used for producing 3 container images.
#
# base - which is thrown away, that contains node and the basic infrastructure.
# devcontainer - which is used for development, and contains the source code and the node_modules.
# dist - which is used for production, and contains the built source code and the node_modules.
ARG NODE_VERSION="20.17"
# Base image
FROM docker.io/node:${NODE_VERSION}-alpine3.19 AS base
## Just reduce unccessary noise in the logs.
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
ENV NEXT_TELEMETRY_DISABLED=1
RUN apk add --no-cache \
bash=5.2.21-r0 \
supervisor=4.2.5-r4 \
make \
build-base
WORKDIR /app
EXPOSE 4200
EXPOSE 3000
COPY var/docker/entrypoint.sh /app/entrypoint.sh
COPY var/docker/supervisord.conf /etc/supervisord.conf
COPY var/docker/supervisord /app/supervisord_available_configs/
COPY .env.example /config/.env
VOLUME /config
LABEL org.opencontainers.image.source=https://github.com/gitroomhq/postiz-app
ENTRYPOINT ["/app/entrypoint.sh"]
# Builder image
FROM base AS devcontainer
COPY nx.json tsconfig.base.json package.json package-lock.json /app/
COPY apps /app/apps/
COPY libraries /app/libraries/
RUN npm ci --no-fund && npx nx run-many --target=build --projects=frontend,backend,workers,cron
LABEL org.opencontainers.image.title="Postiz App (DevContainer)"
# 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/
## Labels at the bottom, because CI will eventually add dates, commit hashes, etc.
LABEL org.opencontainers.image.title="Postiz App (Production)"

View File

@ -20,6 +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": "./var/docker/docker-build.sh",
"docker-create": "./var/docker/docker-create.sh",
"postinstall": "npm run prisma-generate"
},
"private": true,

5
var/docker/docker-build.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
docker rmi localhost/postiz || true
docker build --target dist -t localhost/postiz -f Dockerfile .
docker build --target devcontainer -t localhost/postiz-devcontainer -f Dockerfile .

View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
docker kill postiz || true
docker rm postiz || true
docker create --name postiz -p 3000:3000 -p 4200:4200 localhost/postiz

36
var/docker/entrypoint.sh Executable file
View File

@ -0,0 +1,36 @@
#!/bin/bash
if [[ "$SKIP_CONFIG_CHECK" != "true" ]]; then
echo "symlinking /config/.env into /app/.env"
if [ ! -f /config/.env ]; then
echo "ERROR: No .env file found in /config/.env"
fi
ln -sf /config/.env /app/.env
fi
if [[ "$POSTIZ_APPS" -eq "" ]]; then
echo "POSTIZ_APPS is not set, starting everything!"
POSTIZ_APPS="frontend workers cron backend"
fi
mkdir -p /etc/supervisor.d/
if [[ "$POSTIZ_APPS" == *"frontend"* ]]; then
ln -sf /app/supervisord_available_configs/frontend.conf /etc/supervisor.d/
fi
if [[ $POSTIZ_APPS == *"workers"* ]]; then
ln -sf /app/supervisord_available_configs/workers.conf /etc/supervisor.d/
fi
if [[ $POSTIZ_APPS == *"cron"* ]]; then
ln -sf /app/supervisord_available_configs/cron.conf /etc/supervisor.d/
fi
if [[ $POSTIZ_APPS == *"backend"* ]]; then
ln -sf /app/supervisord_available_configs/backend.conf /etc/supervisor.d/
fi
/usr/bin/supervisord

View File

@ -0,0 +1,10 @@
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
[unix_http_server]
file=/run/supervisord.sock
[include]
files = /etc/supervisor.d/*.conf

View File

@ -0,0 +1,8 @@
[program:backend]
directory=/app
command=npm run start:prod
autostart=true
autorestart=false
redirect_stderr=true
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0

View File

@ -0,0 +1,8 @@
[program:cron]
directory=/app
command=npm run start:prod:cron
autostart=true
autorestart=false
redirect_stderr=true
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0

View File

@ -0,0 +1,8 @@
[program:frontend]
directory=/app
command=npm run start:prod:frontend
autostart=true
autorestart=false
redirect_stderr=true
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0

View File

@ -0,0 +1,8 @@
[program:workers]
directory=/app
command=npm run start:prod:workers
autostart=true
autorestart=false
redirect_stderr=true
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0