diff --git a/Dockerfile b/Dockerfile index 0450a2c..2694b80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -56,7 +56,7 @@ ENV JAVA_HOME=/jre \ RUN npm install -g pm2 && \ apt-get update && \ - apt-get install -y --no-install-recommends nginx wget curl unzip tini && \ + apt-get install -y --no-install-recommends nginx wget curl unzip tini gosu && \ mkdir -p $LIB_PATH && \ wget --no-check-certificate -q -O libs.zip https://github.com/p-vorobyev/spring-boot-starter-telegram/releases/download/1.15.0/libs.zip && \ unzip -q libs.zip -d /tmp/tdlib && \ @@ -89,7 +89,6 @@ COPY --chown=1000:1000 ./web/pm2.json /app/web/ COPY --chown=1000:1000 ./entrypoint.sh . COPY --chown=1000:1000 ./nginx.conf /etc/nginx/nginx.conf -USER 1000 EXPOSE 80 ENTRYPOINT ["/usr/bin/tini", "--"] diff --git a/docker-compose.yaml b/docker-compose.yaml index e58115c..ad6dc9d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -3,8 +3,6 @@ services: container_name: telegram-files image: ghcr.io/jarvis2f/telegram-files:latest restart: always - # Default use 1000:1000 for user:group. If you want to use root, uncomment the line below. - # user: "root" healthcheck: test: [ "CMD", "curl", "-f", "http://127.0.0.1/api/health" ] interval: 30s @@ -12,6 +10,9 @@ services: timeout: 10s start_period: 10s environment: + # PUID and PGID are the user id and group id of the user who owns the files in the mounted volume. + # PUID: 1000 + # PGID: 1000 APP_ENV: "prod" APP_ROOT: "/app/data" TELEGRAM_API_ID: ${TELEGRAM_API_ID} diff --git a/entrypoint.sh b/entrypoint.sh index 93af1f7..a2f313e 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,24 +1,82 @@ -#!/bin/bash +#!/bin/sh -on_terminate() { - echo "Caught termination signal. Shutting down processes..." - kill -TERM "$JAVA_PID" 2>/dev/null - kill -TERM "$PM2_PID" 2>/dev/null - kill -TERM "$NGINX_PID" 2>/dev/null - wait - echo "All processes have been terminated." - exit 0 +set -e # Exit on error + +# Default configuration +PUID=${PUID:-0} +PGID=${PGID:-0} + +# Store PIDs in variables +JAVA_PID="" +PM2_PID="" +NGINX_PID="" + +cleanup() { + echo "Cleaning up processes..." + + # Check and kill each process individually + if [ -n "$JAVA_PID" ]; then + kill -TERM "$JAVA_PID" 2>/dev/null || true + fi + if [ -n "$PM2_PID" ]; then + kill -TERM "$PM2_PID" 2>/dev/null || true + fi + if [ -n "$NGINX_PID" ]; then + kill -TERM "$NGINX_PID" 2>/dev/null || true + fi + + wait || true + echo "All processes have been terminated" + exit 0 } -trap 'on_terminate' 15 2 +setup_permissions() { + if [ "$(id -u)" = "0" ] && [ "$PUID" != "0" ]; then + echo "Setting up directory permissions..." + mkdir -p /.pm2 + chown -R "${PUID}:${PGID}" /app /etc/nginx /var/lib/nginx /var/log/nginx /run/nginx.pid /etc/nginx/nginx.conf /.pm2 + fi +} -java -jar -Djava.library.path=/app/tdlib /app/api.jar & -JAVA_PID=$! +start_services() { + cmd_prefix="" + if [ "$(id -u)" = "0" ] && [ "$PUID" != "0" ]; then + cmd_prefix="gosu ${PUID}:${PGID}" + fi -pm2 start /app/web/pm2.json --no-daemon --silent & -PM2_PID=$! + echo "Starting Java service..." + if [ -n "$cmd_prefix" ]; then + $cmd_prefix java -jar -Djava.library.path=/app/tdlib /app/api.jar & + else + java -jar -Djava.library.path=/app/tdlib /app/api.jar & + fi + JAVA_PID=$! -nginx -g 'daemon off;' & -NGINX_PID=$! + echo "Starting PM2 service..." + if [ -n "$cmd_prefix" ]; then + $cmd_prefix pm2 start /app/web/pm2.json --no-daemon --silent & + else + pm2 start /app/web/pm2.json --no-daemon --silent & + fi + PM2_PID=$! + echo "Starting Nginx service..." + if [ -n "$cmd_prefix" ]; then + $cmd_prefix nginx -g 'daemon off;' & + else + nginx -g 'daemon off;' & + fi + NGINX_PID=$! +} + +# Set up signal handlers +trap cleanup TERM INT + +# Set up permissions +setup_permissions + +# Start services +start_services + +# Wait for all services to complete wait