Move docker components

This commit is contained in:
dragongoose
2024-02-12 22:11:13 -05:00
parent 4468ff0c32
commit 2a519d9b15
7 changed files with 1 additions and 1 deletions

1
docker/.dockerignore Normal file
View File

@ -0,0 +1 @@
node_modules

37
docker/Dockerfile Normal file
View File

@ -0,0 +1,37 @@
# Multi-stage
# 1) Node image for building frontend assets
# 2) nginx stage to serve frontend assets
# Name the node stage "builder"
FROM docker.io/node:16 AS builder
# Set working directory
WORKDIR /app
# Filled with placeholders, later changed and managed
# by the substitute_environment_variables.sh file
ENV SAFETWITCH_BACKEND_DOMAIN SAFETWITCH_BACKEND_DOMAIN_PLACEHOLDER
ENV SAFETWITCH_INSTANCE_DOMAIN SAFETWITCH_INSTANCE_DOMAIN_PLACEHOLDER
ENV SAFETWITCH_HTTPS SAFETWITCH_HTTPS_PLACEHOLDER
ENV SAFETWITCH_DEFAULT_LOCALE SAFETWITCH_DEFAULT_LOCALE_PLACEHOLDER
ENV SAFETWITCH_FALLBACK_LOCALE SAFETWITCH_FALLBACK_LOCALE_PLACEHOLDER
ENV SAFETWITCH_DEFAULT_THEME SAFETWITCH_DEFAULT_THEME_PLACEHOLDER
# Copy all files from current directory to working dir in image
COPY ../ .
# install node modules and build assets
RUN npm i && npm run build
# nginx state for serving content
FROM docker.io/nginx:alpine
COPY ./nginx.conf /etc/nginx/nginx.conf
# Set working directory to nginx asset directory
RUN mkdir /app
# Copy static assets from builder stage
COPY --from=builder /app/dist /app
# Containers run nginx with global directives and daemon off
EXPOSE 8280
# Overriding the default NGINX container behavior
COPY ./substitute_environment_variables.sh ./substitute_environment_variables.sh
RUN chmod +x /substitute_environment_variables.sh
ENTRYPOINT ["/substitute_environment_variables.sh"]

32
docker/docker-compose.yml Normal file
View File

@ -0,0 +1,32 @@
version: "3.7"
services:
safetwitch-frontend:
container_name: safetwitch-frontend
hostname: safetwitch-frontend
restart: always
image: codeberg.org/safetwitch/safetwitch:latest
ports:
- "127.0.0.1:8280:8280"
environment:
- SAFETWITCH_BACKEND_DOMAIN=changeme
- SAFETWITCH_INSTANCE_DOMAIN=changeme
- SAFETWITCH_HTTPS=true
- SAFETWITCH_DEFAULT_LOCALE=en
- SAFETWITCH_FALLBACK_LOCALE=en
safetwitch-backend:
container_name: safetwitch-backend
hostname: safetwitch-backend
read_only: true
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
restart: always
image: codeberg.org/safetwitch/safetwitch-backend:latest
ports:
- "127.0.0.1:7100:7000"
environment:
- PORT=7000
- URL=changeme

17
docker/nginx.conf Normal file
View File

@ -0,0 +1,17 @@
events {}
http {
include mime.types;
server {
listen 8280;
access_log off;
error_log off;
location / {
root /app;
index index.html;
try_files $uri $uri/ /index.html;
}
}
}

View File

@ -0,0 +1,39 @@
#!/bin/sh
ROOT_DIR=/app
# Save old variables
# Get old vars
if [ -f .env.old ]; then
export $(echo $(cat .env.old | sed 's/#.*//g'| xargs) | envsubst)
else
export SAFETWITCH_BACKEND_DOMAIN_PLACEHOLDER=SAFETWITCH_BACKEND_DOMAIN_PLACEHOLDER
export SAFETWITCH_INSTANCE_DOMAIN_PLACEHOLDER=SAFETWITCH_INSTANCE_DOMAIN_PLACEHOLDER
export SAFETWITCH_HTTPS_PLACEHOLDER=SAFETWITCH_HTTPS_PLACEHOLDER
export SAFETWITCH_DEFAULT_THEME_PLACEHOLDER=SAFETWITCH_DEFAULT_THEME_PLACEHOLDER
export SAFETWITCH_DEFAULT_LOCALE_PLACEHOLDER=SAFETWITCH_DEFAULT_LOCALE_PLACEHOLDER
export SAFETWITCH_FALLBACK_LOCALE_PLACEHOLDER=SAFETWITCH_FALLBACK_LOCALE_PLACEHOLDER
fi
# Replace env vars in files served by NGINX
for file in $ROOT_DIR/assets/*.js $ROOT_DIR/index.html;
do
sed -i 's|'${SAFETWITCH_BACKEND_DOMAIN_PLACEHOLDER}'|'${SAFETWITCH_BACKEND_DOMAIN}'|g' $file
sed -i 's|'${SAFETWITCH_INSTANCE_DOMAIN_PLACEHOLDER}'|'${SAFETWITCH_INSTANCE_DOMAIN}'|g' $file
sed -i 's|'${SAFETWITCH_HTTPS_PLACEHOLDER}'|'${SAFETWITCH_HTTPS}'|g' $file
sed -i 's|'${SAFETWITCH_DEFAULT_THEME_PLACEHOLDER}'|'${SAFETWITCH_DEFAULT_THEME}'|g' $file
sed -i 's|'${SAFETWITCH_DEFAULT_LOCALE_PLACEHOLDER}'|'${SAFETWITCH_DEFAULT_LOCALE}'|g' $file
sed -i 's|'${SAFETWITCH_FALLBACK_LOCALE_PLACEHOLDER}'|'${SAFETWITCH_FALLBACK_LOCALE}'|g' $file
# Your other variables here...
done
# Save old variables
echo -e "SAFETWITCH_BACKEND_DOMAIN_PLACEHOLDER=${SAFETWITCH_BACKEND_DOMAIN}" > .env.old
echo -e "SAFETWITCH_INSTANCE_DOMAIN_PLACEHOLDER=${SAFETWITCH_INSTANCE_DOMAIN}" >> .env.old
echo -e "SAFETWITCH_HTTPS_PLACEHOLDER=${SAFETWITCH_HTTPS}" >> .env.old
echo -e "SAFETWITCH_DEFAULT_THEME_PLACEHOLDER=${SAFETWITCH_DEFAULT_THEME}" >> .env.old
echo -e "SAFETWITCH_DEFAULT_LOCALE_PLACEHOLDER=${SAFETWITCH_DEFAULT_LOCALE}" >> .env.old
echo -e "SAFETWITCH_FALLBACK_LOCALE_PLACEHOLDER=${SAFETWITCH_FALLBACK_LOCALE}" >> .env.old
# Starting NGINX
nginx -g 'daemon off;'