diff --git a/DockerInit.sh b/DockerInit.sh deleted file mode 100755 index fb603fb8..00000000 --- a/DockerInit.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/sh -case $1 in - amd64) - ARCH="64" - FNAME="amd64" - ;; - i386) - ARCH="32" - FNAME="i386" - ;; - armv8 | arm64 | aarch64) - ARCH="arm64-v8a" - FNAME="arm64" - ;; - armv7 | arm | arm32) - ARCH="arm32-v7a" - FNAME="arm32" - ;; - armv6) - ARCH="arm32-v6" - FNAME="armv6" - ;; - *) - ARCH="64" - FNAME="amd64" - ;; -esac -mkdir -p build/bin -cd build/bin -wget -q "https://github.com/XTLS/Xray-core/releases/download/v25.10.15/Xray-linux-${ARCH}.zip" -unzip "Xray-linux-${ARCH}.zip" -rm -f "Xray-linux-${ARCH}.zip" geoip.dat geosite.dat -mv xray "xray-linux-${FNAME}" -wget -q https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat -wget -q https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat -wget -q -O geoip_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat -wget -q -O geosite_IR.dat https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat -wget -q -O geoip_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat -wget -q -O geosite_RU.dat https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat -cd ../../ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 56e6a3d2..20c092bf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,7 @@ ARG XRAY_VERSION WORKDIR /app RUN apk add --no-cache wget unzip && mkdir -p /app/bin -COPY xray-tools.sh . +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" diff --git a/docker-compose.yml b/docker-compose.yml index 53f96a97..b9227d73 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,18 +1,58 @@ services: - 3xui: + 3x-ui: build: context: . args: XRAY_VERSION: "v25.10.15" - GEOUPDATE_CRON_SCHEDULE: "0 */6 * * *" container_name: 3xui_app volumes: - $PWD/db/:/etc/x-ui/ - $PWD/cert/:/root/cert/ + - $PWD/geodata/:/opt/geodata/ environment: TZ: "Asia/Tehran" XRAY_VMESS_AEAD_FORCED: "false" XUI_ENABLE_FAIL2BAN: "true" tty: true network_mode: host - restart: unless-stopped \ No newline at end of file + restart: unless-stopped + depends_on: + - geodata-cron + + docker-proxy: + image: tecnativa/docker-socket-proxy + container_name: docker_proxy + restart: unless-stopped + environment: + - CONTAINERS=1 + - POST=1 + - ALLOW_RESTARTS=1 + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + networks: + - docker-internal + + geodata-cron: + build: + context: docker-cron-runner + container_name: geodata_cron + restart: unless-stopped + depends_on: + - docker-proxy + environment: + DOCKER_PROXY_URL: "http://docker-proxy:2375" + TARGET_CONTAINER_NAME: "3xui_app" + # Расписание в формате crond (пример: каждые 6 часов) + # CRON_SCHEDULE: "0 */6 * * *" +# CRON_SCHEDULE: "*/1 * * * *" + CRON_SCHEDULE: "0 */6 * * *" + SHARED_VOLUME_PATH: "/opt/geodata" + volumes: + - $PWD/geodata/:/opt/geodata/ + - ./xray-tools.sh:/usr/local/bin/xray-tools.sh:ro + networks: + - docker-internal + +networks: + docker-internal: + driver: bridge \ No newline at end of file diff --git a/docker-cron-runner/Dockerfile b/docker-cron-runner/Dockerfile new file mode 100644 index 00000000..77f069e0 --- /dev/null +++ b/docker-cron-runner/Dockerfile @@ -0,0 +1,15 @@ +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 + +# Копируем entrypoint +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +RUN chmod +x xray-tools.sh + + +# cron внутри alpine использует crond (busybox) +CMD ["/entrypoint.sh"] \ No newline at end of file diff --git a/docker-cron-runner/entrypoint.sh b/docker-cron-runner/entrypoint.sh new file mode 100644 index 00000000..f580f647 --- /dev/null +++ b/docker-cron-runner/entrypoint.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env sh +set -e + +: "${CRON_SCHEDULE:=0 */6 * * *}" +: "${DOCKER_PROXY_URL:?DOCKER_PROXY_URL is required}" +: "${TARGET_CONTAINER_NAME:?TARGET_CONTAINER_NAME is required}" +: "${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 + +echo "Starting crond with schedule: ${CRON_SCHEDULE}" +mkdir -p /var/log +touch /var/log/cron.log + +bash $CRON_JOB_SCRIPT + +# Запускаем crond в foreground +exec crond -f -l 2 \ No newline at end of file diff --git a/xray-tools.sh b/docker-cron-runner/xray-tools.sh similarity index 100% rename from xray-tools.sh rename to docker-cron-runner/xray-tools.sh diff --git a/x-ui.sh b/x-ui.sh index d9112b50..f56eca35 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -6,7 +6,7 @@ blue='\033[0;34m' yellow='\033[0;33m' plain='\033[0m' -source xray-tools.sh +source docker-cron-runner/xray-tools.sh #Add some basic function here function LOGD() {