Skip to content

Commit 5f0f1c4

Browse files
committed
Added scripts and documentation to handle network changes on the docker image (endpoint address update on the website when the network has changed)
1 parent 44fb866 commit 5f0f1c4

File tree

7 files changed

+251
-1
lines changed

7 files changed

+251
-1
lines changed

WebInterface/src/api/server-info.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313
function getServerIPs() {
1414
$ips = array();
1515

16+
// Check if IP configuration file exists first (highest priority)
17+
$configFile = __DIR__ . '/../../config/ip-config.json';
18+
if (file_exists($configFile)) {
19+
$configData = json_decode(file_get_contents($configFile), true);
20+
if ($configData) {
21+
// Use config file values if they exist and are valid
22+
$localIP = isset($configData['local_ip']) && filter_var($configData['local_ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
23+
? $configData['local_ip'] : null;
24+
$externalIP = isset($configData['external_ip']) && filter_var($configData['external_ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
25+
? $configData['external_ip'] : null;
26+
27+
if ($localIP && $externalIP) {
28+
// Both IPs are valid in config, use them
29+
return array('local' => $localIP, 'external' => $externalIP);
30+
} else if ($localIP) {
31+
// Only local IP is valid in config, detect external dynamically
32+
return array('local' => $localIP, 'external' => getExternalIP());
33+
}
34+
}
35+
}
36+
37+
// Fallback to dynamic detection if config file doesn't exist or has invalid data
38+
return detectIPs();
39+
}
40+
41+
function detectIPs() {
42+
$ips = array();
43+
1644
// Get local network IP addresses
1745
$localIPs = array();
1846

@@ -30,7 +58,7 @@ function isValidLocalIP($ip) {
3058
!isDockerContainerIP($ip);
3159
}
3260

33-
// Method 0: Check if HOST_IP is provided via environment variable (highest priority)
61+
// Method 0: Check if HOST_IP is provided via environment variable (lower priority than config file)
3462
$hostIP = getenv('HOST_IP');
3563
if ($hostIP && filter_var($hostIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
3664
$localIPs[] = $hostIP;

WebInterface/start-services.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ if "%HOST_IP%"=="" (
9393
echo [OK] Detected host IP: %HOST_IP%
9494
)
9595

96+
echo [INFO] IP addresses will be detected automatically by the website
97+
9698
REM Start the container
9799
echo Starting container...
98100
docker run -d ^

WebInterface/start-services.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ else
132132
echo "✅ Detected host IP: $HOST_IP"
133133
fi
134134

135+
echo "📡 IP addresses will be detected automatically by the website"
136+
135137
# Start the unified container using docker run for better Docker Desktop display
136138
echo "🚀 Starting unified container..."
137139
docker run -d \

WebInterface/update-network-ip.bat

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@echo off
2+
3+
echo Updating network IP in Docker container...
4+
5+
REM Get local IP using basic Windows commands
6+
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr "IPv4" ^| findstr "192.168"') do (
7+
for /f "tokens=*" %%b in ("%%a") do set LOCAL_IP=%%b
8+
)
9+
10+
REM Fallback to other private ranges if 192.168 not found
11+
if "%LOCAL_IP%"=="" (
12+
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr "IPv4" ^| findstr "10\."') do (
13+
for /f "tokens=*" %%b in ("%%a") do set LOCAL_IP=%%b
14+
)
15+
)
16+
17+
REM Set fallback if no IP detected
18+
if "%LOCAL_IP%"=="" set LOCAL_IP=127.0.0.1
19+
20+
echo Detected local IP: %LOCAL_IP%
21+
22+
REM Update IP configuration directly in container
23+
docker exec multibarcode-webinterface bash -c "mkdir -p /var/www/html/config"
24+
25+
docker exec multibarcode-webinterface bash -c "cat > /var/www/html/config/ip-config.json << 'EOF'
26+
{
27+
\"local_ip\": \"%LOCAL_IP%\",
28+
\"external_ip\": \"Unable to detect\",
29+
\"last_updated\": \"$(date -u +%%Y-%%m-%%dT%%H:%%M:%%SZ)\",
30+
\"detection_method\": \"batch_update\"
31+
}
32+
EOF"
33+
34+
REM Restart container with new HOST_IP environment variable
35+
docker stop multibarcode-webinterface
36+
docker rm multibarcode-webinterface
37+
38+
docker run -d --name multibarcode-webinterface -p 3500:3500 -e MYSQL_ROOT_PASSWORD=root_password -e MYSQL_DATABASE=barcode_wms -e MYSQL_USER=wms_user -e MYSQL_PASSWORD=wms_password -e DB_HOST=127.0.0.1 -e DB_NAME=barcode_wms -e DB_USER=wms_user -e DB_PASS=wms_password -e WEB_PORT=3500 -e HOST_IP=%LOCAL_IP% -e EXPOSE_PHPMYADMIN=false -e EXPOSE_MYSQL=false -v multibarcode_mysql_data:/var/lib/mysql --restart unless-stopped multibarcode-webinterface:latest
39+
40+
echo Network IP updated to: %LOCAL_IP%
41+
echo Container restarted successfully.

WebInterface/update-network-ip.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
echo "Updating network IP in Docker container..."
4+
5+
# Get local IP using standard commands
6+
LOCAL_IP=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep '^192\.168\.' | head -1)
7+
8+
# Fallback to other private ranges
9+
if [ -z "$LOCAL_IP" ]; then
10+
LOCAL_IP=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^(10\.|172\.(1[6-9]|2[0-9]|3[0-1]))' | head -1)
11+
fi
12+
13+
# Final fallback
14+
if [ -z "$LOCAL_IP" ]; then
15+
LOCAL_IP="127.0.0.1"
16+
fi
17+
18+
echo "Detected local IP: $LOCAL_IP"
19+
20+
# Update IP configuration directly in container
21+
docker exec multibarcode-webinterface bash -c "mkdir -p /var/www/html/config"
22+
23+
docker exec multibarcode-webinterface bash -c "cat > /var/www/html/config/ip-config.json << 'EOF'
24+
{
25+
\"local_ip\": \"$LOCAL_IP\",
26+
\"external_ip\": \"Unable to detect\",
27+
\"last_updated\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",
28+
\"detection_method\": \"shell_update\"
29+
}
30+
EOF"
31+
32+
# Restart container with new HOST_IP environment variable
33+
docker stop multibarcode-webinterface
34+
docker rm multibarcode-webinterface
35+
36+
docker run -d --name multibarcode-webinterface -p 3500:3500 -e MYSQL_ROOT_PASSWORD=root_password -e MYSQL_DATABASE=barcode_wms -e MYSQL_USER=wms_user -e MYSQL_PASSWORD=wms_password -e DB_HOST=127.0.0.1 -e DB_NAME=barcode_wms -e DB_USER=wms_user -e DB_PASS=wms_password -e WEB_PORT=3500 -e HOST_IP="$LOCAL_IP" -e EXPOSE_PHPMYADMIN=false -e EXPOSE_MYSQL=false -v multibarcode_mysql_data:/var/lib/mysql --restart unless-stopped multibarcode-webinterface:latest
37+
38+
echo "Network IP updated to: $LOCAL_IP"
39+
echo "Container restarted successfully."

wiki/11-Managing-IP-Changes.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Managing IP Changes
2+
3+
When you connect to a new network (different WiFi, mobile hotspot, or office network), your computer's IP address changes. This means the endpoint URLs in the AI MultiBarcode Capture app need to be updated to maintain connectivity with the web management system.
4+
5+
## 🔧 Quick Solution
6+
7+
The system includes automatic IP update scripts that detect your new network IP and update the Docker container configuration.
8+
9+
### Windows Users
10+
11+
Run the following script in the `WebInterface` directory:
12+
13+
```batch
14+
update-network-ip.bat
15+
```
16+
17+
### Linux/macOS Users
18+
19+
Run the following script in the `WebInterface` directory:
20+
21+
```bash
22+
./update-network-ip.sh
23+
```
24+
25+
## 📋 What These Scripts Do
26+
27+
1. **Detect New IP**: Automatically detect your current network IP address
28+
2. **Update Container**: Restart the Docker container with the new IP configuration
29+
3. **Preserve Data**: All your barcode sessions and data remain intact
30+
4. **Update Endpoints**: The web interface will show the correct new endpoint URLs
31+
32+
## 🎯 When to Use
33+
34+
Use these scripts whenever you:
35+
36+
- Connect to a different WiFi network
37+
- Switch from WiFi to mobile hotspot
38+
- Move between office locations
39+
- Experience connectivity issues with the Android app
40+
41+
## ⚡ Step-by-Step Process
42+
43+
### For Windows:
44+
45+
1. Open Command Prompt or PowerShell
46+
2. Navigate to the WebInterface directory:
47+
```
48+
cd path\to\AI_MultiBarcode_Capture\WebInterface
49+
```
50+
3. Run the update script:
51+
```
52+
update-network-ip.bat
53+
```
54+
4. Wait for the script to complete (typically 30-60 seconds)
55+
56+
### For Linux/macOS:
57+
58+
1. Open Terminal
59+
2. Navigate to the WebInterface directory:
60+
```
61+
cd path/to/AI_MultiBarcode_Capture/WebInterface
62+
```
63+
3. Run the update script:
64+
```
65+
./update-network-ip.sh
66+
```
67+
4. Wait for the script to complete (typically 30-60 seconds)
68+
69+
## 📱 Updating Your Android App
70+
71+
After running the IP update script:
72+
73+
1. **Open the web interface** at `http://localhost:3500`
74+
2. **Click the Settings (⚙️) button** in the top-right corner
75+
3. **Click "🔗 Endpoint Configuration"**
76+
4. **Copy the new Local Network Endpoint** (it will show your new IP address)
77+
5. **Open the AI MultiBarcode Capture app** on your Android device
78+
6. **Go to Settings → Server Configuration**
79+
7. **Paste the new endpoint URL**
80+
8. **Test the connection** to verify it works
81+
82+
## 🔍 Verifying the Update
83+
84+
After running the script, you can verify it worked by:
85+
86+
1. **Check the script output** - it should show "Network IP updated to: [YOUR_NEW_IP]"
87+
2. **Visit the web interface** at `http://localhost:3500`
88+
3. **Check the endpoint configuration** - it should show your new IP address
89+
4. **Test Android connectivity** by scanning a barcode
90+
91+
## 🛠️ Technical Details
92+
93+
The scripts perform these operations:
94+
95+
1. **IP Detection**: Use system commands (`ipconfig` on Windows, `hostname -I` on Linux) to detect the current network IP
96+
2. **Container Restart**: Stop and remove the current Docker container
97+
3. **Reconfiguration**: Start a new container with the updated `HOST_IP` environment variable
98+
4. **Data Preservation**: Reuse the same MySQL data volume to preserve all sessions and barcodes
99+
100+
## ⚠️ Important Notes
101+
102+
- **No Data Loss**: Your barcode sessions and data are preserved during the update
103+
- **Brief Downtime**: The web interface will be unavailable for 30-60 seconds during the container restart
104+
- **Automatic Detection**: The scripts automatically detect common private IP ranges (192.168.x.x, 10.x.x.x, 172.x.x.x)
105+
- **No User Interaction**: The scripts run completely automatically without prompting for input
106+
107+
## 🔧 Troubleshooting
108+
109+
### Script Won't Run
110+
- **Windows**: Ensure you're running as Administrator if you get permission errors
111+
- **Linux/macOS**: Make sure the script is executable with `chmod +x update-network-ip.sh`
112+
113+
### Container Won't Start
114+
- **Check Docker**: Ensure Docker Desktop is running
115+
- **Port Conflicts**: Make sure port 3500 isn't being used by another application
116+
- **Check Logs**: Run `docker logs multibarcode-webinterface` to see error messages
117+
118+
### IP Not Detected
119+
- The script will fall back to `127.0.0.1` if it can't detect your IP
120+
- You can manually check your IP with `ipconfig` (Windows) or `ip addr` (Linux)
121+
- Ensure you're connected to a network with a valid IP address
122+
123+
### Android App Still Can't Connect
124+
- **Double-check the endpoint URL** in the Android app settings
125+
- **Verify both devices are on the same network**
126+
- **Check firewall settings** that might block connections
127+
- **Try the internet endpoint** if local network endpoint doesn't work
128+
129+
## 📚 Related Documentation
130+
131+
- **[Docker WMS Deployment](10-Docker-WMS-Deployment.md)** - Initial setup and deployment
132+
- **[Android App Configuration](07-Android-App-Configuration.md)** - Configuring the mobile app
133+
- **[Troubleshooting Guide](15-Troubleshooting-Guide.md)** - General troubleshooting help
134+
135+
---
136+
137+
**💡 Tip**: Consider bookmarking this page or keeping the script location handy, as network changes are common when working with mobile devices in different locations.

wiki/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This wiki provides comprehensive documentation for the AI MultiBarcode Capture s
2020

2121
### 🌐 Web Management System (WMS)
2222
- **[Docker WMS Deployment](10-Docker-WMS-Deployment.md)** - Complete guide to deploy and use the web management system with Docker
23+
- **[Managing IP Changes](11-Managing-IP-Changes.md)** - Update endpoint configuration when connecting to new networks
2324

2425
### 🔧 Advanced Configuration
2526
- **[Troubleshooting Guide](15-Troubleshooting-Guide.md)** - Common issues and solutions

0 commit comments

Comments
 (0)