Skip to content

Commit

Permalink
Merge pull request #205 from jamesread/dockerfile
Browse files Browse the repository at this point in the history
feat: add dockerfile for production and developer use
  • Loading branch information
jonathan-irvin authored Sep 9, 2024
2 parents 7a81d37 + 64b43e8 commit d93d644
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions .github/workflows/build-containers.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 58 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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)"
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions var/docker/docker-build.sh
Original file line number Diff line number Diff line change
@@ -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 .
5 changes: 5 additions & 0 deletions var/docker/docker-create.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions var/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions var/docker/supervisord.conf
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions var/docker/supervisord/backend.conf
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions var/docker/supervisord/cron.conf
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions var/docker/supervisord/frontend.conf
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions var/docker/supervisord/workers.conf
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d93d644

Please sign in to comment.