diff --git a/DockerEntrypoint.sh b/DockerEntrypoint.sh index 277026b8..95c86869 100644 --- a/DockerEntrypoint.sh +++ b/DockerEntrypoint.sh @@ -1,5 +1,12 @@ #!/bin/sh +FINISH_FILE="$GEODATA_DIR/cron-job-finished.txt" + +while [ ! -f "$FINISH_FILE" ]; do + echo "Still waiting... (looking for $FINISH_FILE)" + sleep 10 +done + # Start fail2ban [ "$XUI_ENABLE_FAIL2BAN" = "true" ] && fail2ban-client -x start diff --git a/Dockerfile b/Dockerfile index 20c092bf..ab861c8c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,22 +19,6 @@ ENV CGO_ENABLED=1 ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE" RUN go build -ldflags "-w -s" -o build/x-ui main.go -# ======================================================== -# Stage: Xray downloader -# ======================================================== -FROM alpine AS xray-downloader - -ARG TARGETARCH -ARG XRAY_VERSION - -WORKDIR /app -RUN apk add --no-cache wget unzip && mkdir -p /app/bin - -COPY docker-cron-runner/xray-tools.sh . -RUN chmod +x /app/xray-tools.sh && \ - ./xray-tools.sh install_xray_core "$TARGETARCH" "/app/bin" "$XRAY_VERSION" && \ - ./xray-tools.sh update_geodata_in_docker "/app/bin" - # ======================================================== # Stage: Final Image of 3x-ui # ======================================================== @@ -51,8 +35,6 @@ RUN apk add --no-cache \ COPY DockerEntrypoint.sh /app/ COPY --from=builder /app/build/ /app/ COPY --from=builder /app/x-ui.sh /usr/bin/x-ui -COPY --from=xray-downloader /app/bin /app/bin - # Configure fail2ban RUN rm -f /etc/fail2ban/jail.d/alpine-ssh.conf \ diff --git a/docker-compose.yml b/docker-compose.yml index b9227d73..a9d69373 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,17 +2,16 @@ services: 3x-ui: build: context: . - args: - XRAY_VERSION: "v25.10.15" container_name: 3xui_app volumes: - $PWD/db/:/etc/x-ui/ - $PWD/cert/:/root/cert/ - - $PWD/geodata/:/opt/geodata/ + - $PWD/geodata/:/app/bin environment: TZ: "Asia/Tehran" XRAY_VMESS_AEAD_FORCED: "false" XUI_ENABLE_FAIL2BAN: "true" + GEODATA_DIR: "/app/bin" tty: true network_mode: host restart: unless-stopped @@ -35,21 +34,22 @@ services: geodata-cron: build: context: docker-cron-runner + args: + XRAY_VERSION: "v25.10.15" + XRAY_BUILD_DIR: "/app/xray" container_name: geodata_cron restart: unless-stopped depends_on: - docker-proxy environment: + TZ: "UTC" DOCKER_PROXY_URL: "http://docker-proxy:2375" TARGET_CONTAINER_NAME: "3xui_app" - # Расписание в формате crond (пример: каждые 6 часов) - # CRON_SCHEDULE: "0 */6 * * *" -# CRON_SCHEDULE: "*/1 * * * *" +# CRON_SCHEDULE: "*/5 * * * *" CRON_SCHEDULE: "0 */6 * * *" - SHARED_VOLUME_PATH: "/opt/geodata" + SHARED_VOLUME_PATH: "/app/bin" volumes: - - $PWD/geodata/:/opt/geodata/ - - ./xray-tools.sh:/usr/local/bin/xray-tools.sh:ro + - $PWD/geodata/:/app/bin/ networks: - docker-internal diff --git a/docker-cron-runner/Dockerfile b/docker-cron-runner/Dockerfile index 77f069e0..b3c1fcc8 100644 --- a/docker-cron-runner/Dockerfile +++ b/docker-cron-runner/Dockerfile @@ -1,15 +1,29 @@ FROM alpine:3.20 -WORKDIR app -COPY xray-tools.sh . -# Нужные утилиты: curl + bash (если xray-tools.sh написан под bash) -RUN apk add --no-cache curl bash ca-certificates tzdata +ARG TARGETARCH +ARG XRAY_VERSION +ARG XRAY_BUILD_DIR -# Копируем entrypoint -COPY entrypoint.sh /entrypoint.sh -RUN chmod +x /entrypoint.sh -RUN chmod +x xray-tools.sh +WORKDIR /app + +RUN apk add --no-cache \ + wget \ + unzip \ + curl \ + bash \ + ca-certificates \ + tzdata -# cron внутри alpine использует crond (busybox) -CMD ["/entrypoint.sh"] \ No newline at end of file +COPY xray-tools.sh entrypoint.sh cron-job-script.sh ./ + +#RUN mkdir -p "$XRAY_BUILD_DIR" +RUN chmod +x /app/xray-tools.sh /app/entrypoint.sh /app/cron-job-script.sh \ + && mkdir -p "$XRAY_BUILD_DIR" \ + && ./xray-tools.sh install_xray_core "$TARGETARCH" "$XRAY_BUILD_DIR" "$XRAY_VERSION" \ + && ./xray-tools.sh update_geodata_in_docker "$XRAY_BUILD_DIR" + +ENV XRAY_BUILD_DIR=${XRAY_BUILD_DIR} + +#CMD ["/app/entrypoint.sh"] \ +ENTRYPOINT ["/app/entrypoint.sh"] \ No newline at end of file diff --git a/docker-cron-runner/cron-job-script.sh b/docker-cron-runner/cron-job-script.sh new file mode 100644 index 00000000..ec2eff8b --- /dev/null +++ b/docker-cron-runner/cron-job-script.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env sh +set -eu + +echo "[$(date)] Starting geodata update..." + +FINISHED_FLAG="${SHARED_VOLUME_PATH}/cron-job-finished.txt" + +if [ -f "$FINISHED_FLAG" ]; then + rm -f "$FINISHED_FLAG" +fi + +/app/xray-tools.sh update_geodata_in_docker "${SHARED_VOLUME_PATH}" +touch "$FINISHED_FLAG" + +echo "[$(date)] Geodata update finished, restarting container..." + +HTTP_CODE=$( + curl -s -X POST \ + "${DOCKER_PROXY_URL}/containers/${TARGET_CONTAINER_NAME}/restart" \ + -o /dev/null -w "%{http_code}" +) + +echo "[$(date)] Restart request sent, HTTP status: ${HTTP_CODE}" \ No newline at end of file diff --git a/docker-cron-runner/entrypoint.sh b/docker-cron-runner/entrypoint.sh index f580f647..9d4cc001 100644 --- a/docker-cron-runner/entrypoint.sh +++ b/docker-cron-runner/entrypoint.sh @@ -1,48 +1,25 @@ #!/usr/bin/env sh -set -e + +set -eu : "${CRON_SCHEDULE:=0 */6 * * *}" : "${DOCKER_PROXY_URL:?DOCKER_PROXY_URL is required}" -: "${TARGET_CONTAINER_NAME:?TARGET_CONTAINER_NAME is required}" +: "${TARGET_CONTAINER_NAME:?TARGET_CONTAINER_NAME is required}" # required for cron-job-script.sh for container restart : "${SHARED_VOLUME_PATH:?SHARED_VOLUME_PATH is required}" -# Скрипт, который будет исполняться по крону -CRON_JOB_SCRIPT="/usr/local/bin/run_update_and_restart.sh" - -cat > "$CRON_JOB_SCRIPT" << 'EOF' -#!/usr/bin/env sh -set -e - -echo "[$(date)] Starting geodata update..." - -# Обновление геоданных -/app/xray-tools.sh update_geodata_in_docker "${SHARED_VOLUME_PATH}" - -echo "[$(date)] Geodata update finished, restarting container..." - -# Рестарт контейнера через Docker Socket Proxy -curl -s -X POST \ - "${DOCKER_PROXY_URL}/containers/${TARGET_CONTAINER_NAME}/restart" \ - -o /dev/null -w "%{http_code}\n" - -echo "[$(date)] Restart request sent." -EOF - -chmod +x "$CRON_JOB_SCRIPT" - -# Создаём кронтаб -# Важный момент: переменные окружения надо прокинуть в cron. CRON_ENV_FILE="/env.sh" -env | grep -v '^CRON_SCHEDULE=' | sed 's/^/export /' > "$CRON_ENV_FILE" -# crond не тянет env напрямую, поэтому в крон-строке source env-файла -echo "${CRON_SCHEDULE} . ${CRON_ENV_FILE} && ${CRON_JOB_SCRIPT} >> /var/log/cron.log 2>&1" > /etc/crontabs/root +env | grep -v '^CRON_SCHEDULE=' | sed 's/^/export /' > "$CRON_ENV_FILE" +echo "${CRON_SCHEDULE} . ${CRON_ENV_FILE} && /app/cron-job-script.sh >> /var/log/cron.log 2>&1" > /etc/crontabs/root echo "Starting crond with schedule: ${CRON_SCHEDULE}" + mkdir -p /var/log touch /var/log/cron.log -bash $CRON_JOB_SCRIPT +mkdir -p "$SHARED_VOLUME_PATH" +cp -r "$XRAY_BUILD_DIR"/* "$SHARED_VOLUME_PATH"/ + +touch "$SHARED_VOLUME_PATH/cron-job-finished.txt" # cron job execution imitation -# Запускаем crond в foreground exec crond -f -l 2 \ No newline at end of file