Docker crom runner: cron works and restarts the main container, but shared volume still not in use

This commit is contained in:
Michael S2pac 2025-11-25 00:05:40 +03:00
parent 522a2f7349
commit 4b8275f15f
7 changed files with 108 additions and 45 deletions

View file

@ -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 ../../

View file

@ -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"

View file

@ -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
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

View file

@ -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"]

View file

@ -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

View file

@ -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() {