Skip to main content

Automatic Startup

This page explains how to configure automatic PCH-SIG startup when the server boots.

Startup architecture

When the Windows server (serveur-production) restarts:

Windows Boot


Scheduled Task "PCH-SIG Docker Startup"


Script start-docker.ps1


Docker Machine starts


VirtualBox VM starts


Docker containers restart (restart: always)


Application available (~2-3 min after boot)

Container configuration

docker-compose.yml

All containers have restart: always:

services:
postgres:
restart: always

redis:
restart: always

backend:
restart: always

frontend:
restart: always

This ensures containers restart automatically when Docker starts.


Startup script

start-docker.ps1

File: C:\pch-sig\start-docker.ps1

# Docker Machine startup script for PCH-SIG
# Executed at server boot via scheduled task

$LogFile = "C:\pch-sig\logs\startup.log"

function Write-Log {
param([string]$Message)
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$Timestamp - $Message" | Out-File -Append $LogFile
}

Write-Log "=== PCH-SIG Startup ==="

# Wait for network to be available
Write-Log "Waiting for network..."
Start-Sleep -Seconds 30

# Check if Docker Machine is started
$status = docker-machine status default 2>&1
Write-Log "Docker Machine status: $status"

if ($status -ne "Running") {
Write-Log "Starting Docker Machine..."
docker-machine start default

# Wait for complete startup
Start-Sleep -Seconds 60

$status = docker-machine status default
Write-Log "New status: $status"
}

# Configure Docker environment
Write-Log "Configuring Docker environment..."
& docker-machine env default | Invoke-Expression

# Check containers
Write-Log "Checking containers..."
$containers = docker ps --format "{{.Names}}: {{.Status}}"
Write-Log "Containers: $containers"

# Configure port proxies
Write-Log "Configuring port proxies..."
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=80 connectaddress=192.168.99.100
netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=192.168.99.100

Write-Log "=== Startup complete ==="

Windows Scheduled Task

Create the task

# Create scheduled task
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -File C:\pch-sig\start-docker.ps1"

$Trigger = New-ScheduledTaskTrigger -AtStartup

$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable

Register-ScheduledTask -TaskName "PCH-SIG Docker Startup" -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings -Description "Starts Docker Machine and PCH-SIG containers at boot"

Verify the task

Get-ScheduledTask -TaskName "PCH-SIG Docker Startup"

Run manually

Start-ScheduledTask -TaskName "PCH-SIG Docker Startup"

Remove the task

Unregister-ScheduledTask -TaskName "PCH-SIG Docker Startup" -Confirm:$false

Startup verification

Check logs

Get-Content C:\pch-sig\logs\startup.log -Tail 50

Verify services

After server restart:

# Verify Docker Machine
docker-machine status default

# Verify containers
docker-machine ssh default "docker ps"

# Verify ports
netsh interface portproxy show all

Test the application

# Test frontend
Invoke-WebRequest -Uri "http://localhost:3000" -UseBasicParsing | Select-Object StatusCode

# Test API
Invoke-WebRequest -Uri "http://localhost:8000/api" -UseBasicParsing | Select-Object StatusCode

Troubleshooting

Docker Machine doesn't start

# Check VirtualBox
& "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" list vms

# Start manually
docker-machine start default

Containers don't start

# View logs
docker-machine ssh default "docker logs pch_backend"

# Restart containers
docker-machine ssh default "cd /c/pch-sig/deploy && docker compose restart"

Ports not accessible

# Reconfigure ports
netsh interface portproxy reset

# Re-add redirections
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=80 connectaddress=192.168.99.100
netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=192.168.99.100

Startup monitoring

Monitoring script

# check-status.ps1
while ($true) {
$date = Get-Date -Format "HH:mm:ss"

# Test frontend
try {
$response = Invoke-WebRequest -Uri "http://localhost:3000" -UseBasicParsing -TimeoutSec 5
Write-Host "$date - Frontend: OK" -ForegroundColor Green
} catch {
Write-Host "$date - Frontend: ERROR" -ForegroundColor Red
}

# Test API
try {
$response = Invoke-WebRequest -Uri "http://localhost:8000/api" -UseBasicParsing -TimeoutSec 5
Write-Host "$date - API: OK" -ForegroundColor Green
} catch {
Write-Host "$date - API: ERROR" -ForegroundColor Red
}

Start-Sleep -Seconds 30
}

Next steps