Used shared volume + system optimization

This commit is contained in:
Michael S2pac 2025-11-25 02:10:02 +03:00
parent 4b8275f15f
commit 67d4142c3a
6 changed files with 73 additions and 70 deletions

View file

@ -1,5 +1,12 @@
#!/bin/sh #!/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 # Start fail2ban
[ "$XUI_ENABLE_FAIL2BAN" = "true" ] && fail2ban-client -x start [ "$XUI_ENABLE_FAIL2BAN" = "true" ] && fail2ban-client -x start

View file

@ -19,22 +19,6 @@ ENV CGO_ENABLED=1
ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE" ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE"
RUN go build -ldflags "-w -s" -o build/x-ui main.go 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 # Stage: Final Image of 3x-ui
# ======================================================== # ========================================================
@ -51,8 +35,6 @@ RUN apk add --no-cache \
COPY DockerEntrypoint.sh /app/ COPY DockerEntrypoint.sh /app/
COPY --from=builder /app/build/ /app/ COPY --from=builder /app/build/ /app/
COPY --from=builder /app/x-ui.sh /usr/bin/x-ui COPY --from=builder /app/x-ui.sh /usr/bin/x-ui
COPY --from=xray-downloader /app/bin /app/bin
# Configure fail2ban # Configure fail2ban
RUN rm -f /etc/fail2ban/jail.d/alpine-ssh.conf \ RUN rm -f /etc/fail2ban/jail.d/alpine-ssh.conf \

View file

@ -2,17 +2,16 @@ services:
3x-ui: 3x-ui:
build: build:
context: . context: .
args:
XRAY_VERSION: "v25.10.15"
container_name: 3xui_app container_name: 3xui_app
volumes: volumes:
- $PWD/db/:/etc/x-ui/ - $PWD/db/:/etc/x-ui/
- $PWD/cert/:/root/cert/ - $PWD/cert/:/root/cert/
- $PWD/geodata/:/opt/geodata/ - $PWD/geodata/:/app/bin
environment: environment:
TZ: "Asia/Tehran" TZ: "Asia/Tehran"
XRAY_VMESS_AEAD_FORCED: "false" XRAY_VMESS_AEAD_FORCED: "false"
XUI_ENABLE_FAIL2BAN: "true" XUI_ENABLE_FAIL2BAN: "true"
GEODATA_DIR: "/app/bin"
tty: true tty: true
network_mode: host network_mode: host
restart: unless-stopped restart: unless-stopped
@ -35,21 +34,22 @@ services:
geodata-cron: geodata-cron:
build: build:
context: docker-cron-runner context: docker-cron-runner
args:
XRAY_VERSION: "v25.10.15"
XRAY_BUILD_DIR: "/app/xray"
container_name: geodata_cron container_name: geodata_cron
restart: unless-stopped restart: unless-stopped
depends_on: depends_on:
- docker-proxy - docker-proxy
environment: environment:
TZ: "UTC"
DOCKER_PROXY_URL: "http://docker-proxy:2375" DOCKER_PROXY_URL: "http://docker-proxy:2375"
TARGET_CONTAINER_NAME: "3xui_app" TARGET_CONTAINER_NAME: "3xui_app"
# Расписание в формате crond (пример: каждые 6 часов) # CRON_SCHEDULE: "*/5 * * * *"
# CRON_SCHEDULE: "0 */6 * * *"
# CRON_SCHEDULE: "*/1 * * * *"
CRON_SCHEDULE: "0 */6 * * *" CRON_SCHEDULE: "0 */6 * * *"
SHARED_VOLUME_PATH: "/opt/geodata" SHARED_VOLUME_PATH: "/app/bin"
volumes: volumes:
- $PWD/geodata/:/opt/geodata/ - $PWD/geodata/:/app/bin/
- ./xray-tools.sh:/usr/local/bin/xray-tools.sh:ro
networks: networks:
- docker-internal - docker-internal

View file

@ -1,15 +1,29 @@
FROM alpine:3.20 FROM alpine:3.20
WORKDIR app ARG TARGETARCH
COPY xray-tools.sh . ARG XRAY_VERSION
# Нужные утилиты: curl + bash (если xray-tools.sh написан под bash) ARG XRAY_BUILD_DIR
RUN apk add --no-cache curl bash ca-certificates tzdata
# Копируем entrypoint WORKDIR /app
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh RUN apk add --no-cache \
RUN chmod +x xray-tools.sh wget \
unzip \
curl \
bash \
ca-certificates \
tzdata
# cron внутри alpine использует crond (busybox) COPY xray-tools.sh entrypoint.sh cron-job-script.sh ./
CMD ["/entrypoint.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"]

View file

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

View file

@ -1,48 +1,25 @@
#!/usr/bin/env sh #!/usr/bin/env sh
set -e
set -eu
: "${CRON_SCHEDULE:=0 */6 * * *}" : "${CRON_SCHEDULE:=0 */6 * * *}"
: "${DOCKER_PROXY_URL:?DOCKER_PROXY_URL is required}" : "${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}" : "${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" CRON_ENV_FILE="/env.sh"
env | grep -v '^CRON_SCHEDULE=' | sed 's/^/export /' > "$CRON_ENV_FILE"
# crond не тянет env напрямую, поэтому в крон-строке source env-файла env | grep -v '^CRON_SCHEDULE=' | sed 's/^/export /' > "$CRON_ENV_FILE"
echo "${CRON_SCHEDULE} . ${CRON_ENV_FILE} && ${CRON_JOB_SCRIPT} >> /var/log/cron.log 2>&1" > /etc/crontabs/root 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}" echo "Starting crond with schedule: ${CRON_SCHEDULE}"
mkdir -p /var/log mkdir -p /var/log
touch /var/log/cron.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 exec crond -f -l 2