anchorage_layer_simple/data/start-docker-services.sh
ncantu 31fd2c3c8b Fix: Improve Docker services automatic startup and restart order
**Motivations:**
- Docker services (bitcoind and mempool) were not starting automatically after reboot
- Mempool API was unhealthy because it tried to connect to bitcoind before it was ready
- No restart policy for bitcoind container
- Restart script started mempool before bitcoind, causing connection errors

**Root causes:**
- Bitcoind container had no restart policy (restart: no)
- Restart script started mempool before bitcoind
- No wait for bitcoind RPC to be ready before starting mempool
- No boot startup script for Docker services

**Correctifs:**
- Configured restart policy for bitcoind container: docker update --restart=always
- Improved restart-services-cron.sh:
  - Start/restart bitcoind BEFORE mempool
  - Wait for bitcoind RPC to be ready (max 60s) before starting mempool
  - Detect if container is stopped (start) or running (restart)
  - Use 'docker compose up -d' for mempool instead of 'restart'
- Created start-docker-services.sh:
  - Start bitcoind first
  - Wait for bitcoind RPC to be ready (max 120s at boot)
  - Start mempool after bitcoind
  - Logging to data/start-docker-services.log
- Created docker-services.service:
  - Systemd service to start Docker services at boot
  - Runs after docker.service and network.target
  - Executes start-docker-services.sh

**Evolutions:**
- Automatic startup at boot: Docker services start automatically after reboot
- Guaranteed startup order: Bitcoind always starts before mempool
- Availability check: Wait for bitcoind to be ready before starting mempool
- Restart policy: Bitcoind container restarts automatically if stopped
- Better observability: Detailed logs for diagnosis

**Pages affectées:**
- data/restart-services-cron.sh: Improved startup order and availability checks
- data/start-docker-services.sh: New boot startup script
- data/docker-services.service: New systemd service for automatic startup
- fixKnowledge/docker-services-boot-startup.md: Documentation of improvements
2026-01-28 11:48:25 +01:00

74 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
# Start Docker services at boot (bitcoind and mempool).
# This script ensures bitcoind starts before mempool.
# Can be called from systemd service or cron @reboot.
# Log: data/start-docker-services.log
#
# Local only: no SSH, no remote commands.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
LOG_FILE="$SCRIPT_DIR/start-docker-services.log"
MEMPOOL_COMPOSE="${PROJECT_DIR}/mempool/docker-compose.signet.yml"
BITCOIND_CONTAINER="bitcoin-signet-instance"
log() { echo "$(date -Iseconds) $*" | tee -a "$LOG_FILE"; }
cd "$PROJECT_DIR" || exit 1
log "=== Start Docker services at boot ==="
# Check if docker is available
if ! command -v docker &>/dev/null; then
log "ERROR: docker command not found"
exit 1
fi
# 1. Start bitcoind container (must be first)
if docker ps -a -q -f "name=^${BITCOIND_CONTAINER}$" 2>/dev/null | grep -q .; then
if docker ps -q -f "name=^${BITCOIND_CONTAINER}$" 2>/dev/null | grep -q .; then
log "$BITCOIND_CONTAINER already running"
else
log "Starting $BITCOIND_CONTAINER..."
if docker start "$BITCOIND_CONTAINER" &>/dev/null; then
log " $BITCOIND_CONTAINER started"
# Wait for bitcoind RPC to be ready (max 120 seconds at boot)
max_wait=120
wait_count=0
while [ $wait_count -lt $max_wait ]; do
if docker exec "$BITCOIND_CONTAINER" bitcoin-cli -datadir=/root/.bitcoin getblockchaininfo &>/dev/null; then
log " $BITCOIND_CONTAINER RPC ready after ${wait_count}s"
break
fi
sleep 2
wait_count=$((wait_count + 2))
done
if [ $wait_count -ge $max_wait ]; then
log " WARN: $BITCOIND_CONTAINER RPC not ready after ${max_wait}s (may still be starting)"
fi
else
log " ERROR: Failed to start $BITCOIND_CONTAINER"
fi
fi
else
log "SKIP: $BITCOIND_CONTAINER container not found"
fi
# 2. Start mempool stack (after bitcoind)
if [ -f "$MEMPOOL_COMPOSE" ]; then
log "Starting mempool stack..."
mem_ok=1
(cd "${PROJECT_DIR}/mempool" && docker compose -f docker-compose.signet.yml up -d &>/dev/null) && mem_ok=0
[ "$mem_ok" -ne 0 ] && (cd "${PROJECT_DIR}/mempool" && docker-compose -f docker-compose.signet.yml up -d &>/dev/null) && mem_ok=0
if [ "$mem_ok" -eq 0 ]; then
log " mempool stack started"
else
log " ERROR: Failed to start mempool stack"
fi
else
log "SKIP: mempool compose not found"
fi
log "=== Done ==="
tail -n 100 "$LOG_FILE" > "$LOG_FILE.tmp" && mv "$LOG_FILE.tmp" "$LOG_FILE"