Skip to content

Commit f514299

Browse files
committed
improved s6 startup flow for error handling and log
1 parent bd055b3 commit f514299

File tree

18 files changed

+291
-24
lines changed

18 files changed

+291
-24
lines changed

Dockerfile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ COPY ./s6-overlay/ /etc/s6-overlay/
292292
RUN chmod -R 755 /etc/s6-overlay/s6-rc.d
293293
RUN chmod +x /etc/s6-overlay/s6-rc.d/*/run
294294
RUN chmod +x /etc/s6-overlay/s6-rc.d/*/up
295+
RUN chmod +x /etc/s6-overlay/s6-rc.d/*/finish 2>/dev/null || true
295296
RUN chmod +x /etc/s6-overlay/scripts/*
296297

297298
# Set environment variables
@@ -330,9 +331,18 @@ RUN chmod +x /usr/local/bin/register-ui-events.sh
330331
# Expose port
331332
EXPOSE 8080
332333

334+
# Add health check
335+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
336+
CMD curl -f http://localhost:8080/v1/sys/health || exit 1
337+
333338
ENV PATH="/command:$PATH"
334339

335-
ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=60000
340+
ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=300000
341+
342+
# Configure s6-overlay behavior on failures
343+
ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2
344+
ENV S6_KILL_GRACETIME=3000
345+
ENV S6_KILL_FINISH_MAXTIME=5000
336346

337347
# Set s6 as entrypoint
338348
ENTRYPOINT ["/init"]

README.MD

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ CasaIMG provides environment variables to configure how new containers started b
8282
| REF_NET | Docker network to attach new containers to | `meta` | `bridge` |
8383
| REF_PORT | Default published port for container web interfaces | `80` | Container's default |
8484
| REF_DOMAIN | Base domain for automatic subdomain generation | `domain.com` | none |
85+
| PUID | User ID for file permissions | `1000` | `1000` |
86+
| PGID | Group ID for file permissions | `1000` | `1000` |
87+
| SKIP_CHOWN | Skip recursive ownership changes for faster startup | `false` | `true` |
88+
| S6_CMD_WAIT_FOR_SERVICES_MAXTIME | Service startup timeout in milliseconds | `300000` | `300000` |
89+
| S6_BEHAVIOUR_IF_STAGE2_FAILS | Container behavior on service failure (2=exit) | `1` | `2` |
8590

8691
### Domain Configuration Example
8792

@@ -123,8 +128,6 @@ services:
123128
- REF_PORT defines the default external port for web interfaces of new containers
124129
- Container-specific ports can still be configured individually through CasaOS interface
125130

126-
Would you like me to clarify or expand any part of this section?
127-
128131
# Development Setup
129132

130133
## Repository Structure
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/command/with-contenv bash
2+
3+
echo "[CasaOS-AppManagement] Service crashed or failed to start (exit code: $1)"
4+
5+
# If the service failed to start or crashed unexpectedly, kill the container
6+
if [ "$1" -ne 0 ] && [ "$1" -ne 256 ]; then
7+
echo "[CasaOS-AppManagement] Critical service failure - terminating container"
8+
# Signal s6-overlay to terminate the container
9+
/command/s6-svscanctl -t /var/run/s6/services
10+
fi
11+
12+
exit 0

s6-overlay/s6-rc.d/casaos-app-management/run

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/command/with-contenv bash
22

3+
echo "[CasaOS-AppManagement] Starting CasaOS App Management service..."
4+
echo "[CasaOS-AppManagement] Waiting for dependencies..."
5+
36
# Get the message bus URL
47
runtime_file="/var/run/casaos/message-bus.url"
58

9+
echo "[CasaOS-AppManagement] Waiting for message bus to be ready..."
610
while [ ! -f "$runtime_file" ]; do
711
sleep 1
8-
echo "Casa Main waiting for message bus URL..."
12+
echo "[CasaOS-AppManagement] Still waiting for message bus URL..."
913
done
14+
echo "[CasaOS-AppManagement] Message bus is ready!"
1015

1116
# Wait for /var/run/casaos/routes.json to be created and contains local_storage
17+
echo "[CasaOS-AppManagement] Waiting for local storage service to be registered..."
1218
while [ ! -f /var/run/casaos/routes.json ] || ! grep -q "local_storage" /var/run/casaos/routes.json; do
13-
echo "Waiting for /var/run/casaos/routes.json to be created and contains local_storage..."
19+
echo "[CasaOS-AppManagement] Still waiting for local storage registration..."
1420
sleep 1
1521
done
22+
echo "[CasaOS-AppManagement] Local storage service is registered!"
1623

1724
# Define log filter function
1825
filter_logs() {
@@ -79,4 +86,7 @@ filter_logs() {
7986
done
8087
}
8188

82-
exec gosu "$PUID:$DOCKER_GID" /usr/local/bin/casaos-app-management 2>&1 | filter_logs
89+
echo "[CasaOS-AppManagement] Starting app management server with Docker access (UID:DOCKER_GID $PUID:$DOCKER_GID)..."
90+
echo "[CasaOS-AppManagement] App management service ready - filtering logs for clarity"
91+
touch /var/run/casaos-status/app-management-started
92+
exec gosu "$PUID:$DOCKER_GID" /usr/local/bin/casaos-app-management 2>&1 | filter_logs "app-management"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/command/with-contenv bash
2+
3+
echo "[CasaOS-Gateway] Service crashed or failed to start (exit code: $1)"
4+
5+
# If the service failed to start or crashed unexpectedly, kill the container
6+
if [ "$1" -ne 0 ] && [ "$1" -ne 256 ]; then
7+
echo "[CasaOS-Gateway] Critical service failure - terminating container"
8+
# Signal s6-overlay to terminate the container
9+
/command/s6-svscanctl -t /var/run/s6/services
10+
fi
11+
12+
exit 0

s6-overlay/s6-rc.d/casaos-gateway/run

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/command/with-contenv bash
22

3+
echo "[CasaOS-Gateway] Starting CasaOS Gateway service..."
4+
echo "[CasaOS-Gateway] Initializing routes configuration..."
35
echo "{}" > /var/run/casaos/routes.json
46
chown "$PUID:$PGID" /var/run/casaos/routes.json
7+
echo "[CasaOS-Gateway] Routes configuration initialized at /var/run/casaos/routes.json"
8+
echo "[CasaOS-Gateway] Starting gateway server as UID:GID $PUID:$PGID..."
59

610
# Define log filter function
711
filter_logs() {
@@ -56,4 +60,6 @@ filter_logs() {
5660
done
5761
}
5862

63+
echo "[CasaOS-Gateway] Gateway service ready - filtering logs for clarity"
64+
touch /var/run/casaos-status/gateway-started
5965
exec gosu "$PUID:$PGID" /usr/local/bin/casaos-gateway 2>&1 | filter_logs
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/command/with-contenv bash
2+
3+
echo "[CasaOS-LocalStorage] Service crashed or failed to start (exit code: $1)"
4+
5+
# If the service failed to start or crashed unexpectedly, kill the container
6+
if [ "$1" -ne 0 ] && [ "$1" -ne 256 ]; then
7+
echo "[CasaOS-LocalStorage] Critical service failure - terminating container"
8+
# Signal s6-overlay to terminate the container
9+
/command/s6-svscanctl -t /var/run/s6/services
10+
fi
11+
12+
exit 0

s6-overlay/s6-rc.d/casaos-local-storage/run

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
#!/command/with-contenv bash
22

3+
echo "[CasaOS-LocalStorage] Starting CasaOS Local Storage service..."
4+
echo "[CasaOS-LocalStorage] Waiting for dependencies..."
5+
36
# Get the message bus URL
47
runtime_file="/var/run/casaos/message-bus.url"
58

9+
echo "[CasaOS-LocalStorage] Waiting for message bus to be ready..."
610
while [ ! -f "$runtime_file" ]; do
711
sleep 1
8-
echo "Casa Main waiting for message bus URL..."
12+
echo "[CasaOS-LocalStorage] Still waiting for message bus URL..."
913
done
14+
echo "[CasaOS-LocalStorage] Message bus is ready!"
1015

1116
# Wait for /var/run/casaos/routes.json to be created
17+
echo "[CasaOS-LocalStorage] Waiting for gateway routes configuration..."
1218
while [ ! -f /var/run/casaos/routes.json ]; do
13-
echo "Waiting for /var/run/casaos/routes.json to be created..."
19+
echo "[CasaOS-LocalStorage] Still waiting for gateway routes..."
1420
sleep 1
1521
done
22+
echo "[CasaOS-LocalStorage] Gateway routes found!"
1623

1724
# Wait for main service to be ready
25+
echo "[CasaOS-LocalStorage] Waiting for main service to be ready..."
1826
while [ ! -f /var/run/casaos/casaos.url ]; do
27+
echo "[CasaOS-LocalStorage] Still waiting for main service..."
1928
sleep 1
2029
done
30+
echo "[CasaOS-LocalStorage] Main service is ready!"
2131

2232
# Define log filter function
2333
filter_logs() {
@@ -84,4 +94,7 @@ filter_logs() {
8494
done
8595
}
8696

87-
exec gosu "$PUID:$PGID" /usr/local/bin/casaos-local-storage 2>&1 | filter_logs
97+
echo "[CasaOS-LocalStorage] Starting local storage server as UID:GID $PUID:$PGID..."
98+
echo "[CasaOS-LocalStorage] Local storage service ready - filtering logs for clarity"
99+
touch /var/run/casaos-status/local-storage-started
100+
exec gosu "$PUID:$PGID" /usr/local/bin/casaos-local-storage 2>&1 | filter_logs "local-storage"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/command/with-contenv bash
2+
3+
echo "[CasaOS-Main] Service crashed or failed to start (exit code: $1)"
4+
5+
# If the service failed to start or crashed unexpectedly, kill the container
6+
if [ "$1" -ne 0 ] && [ "$1" -ne 256 ]; then
7+
echo "[CasaOS-Main] Critical service failure - terminating container"
8+
# Signal s6-overlay to terminate the container
9+
/command/s6-svscanctl -t /var/run/s6/services
10+
fi
11+
12+
exit 0

s6-overlay/s6-rc.d/casaos-main/run

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
#!/command/with-contenv bash
22

3+
echo "[CasaOS-Main] Starting CasaOS Main service..."
4+
echo "[CasaOS-Main] Waiting for dependencies..."
5+
36
# Get the message bus URL
47
runtime_file="/var/run/casaos/message-bus.url"
58

9+
echo "[CasaOS-Main] Waiting for message bus to be ready ($runtime_file)..."
610
while [ ! -f "$runtime_file" ]; do
711
sleep 1
8-
echo "Casa Main waiting for message bus URL..."
12+
echo "[CasaOS-Main] Still waiting for message bus URL..."
913
done
14+
echo "[CasaOS-Main] Message bus is ready!"
1015

1116
# Wait for /var/run/casaos/routes.json to be created
17+
echo "[CasaOS-Main] Waiting for gateway routes configuration..."
1218
while [ ! -f /var/run/casaos/routes.json ]; do
13-
echo "Waiting for /var/run/casaos/routes.json to be created..."
19+
echo "[CasaOS-Main] Still waiting for /var/run/casaos/routes.json..."
1420
sleep 1
1521
done
22+
echo "[CasaOS-Main] Gateway routes configuration found!"
1623

1724
# Define log filter function
1825
filter_logs() {
@@ -79,4 +86,7 @@ filter_logs() {
7986
done
8087
}
8188

82-
exec gosu "$PUID:$PGID" /usr/local/bin/casaos-main 2>&1 | filter_logs
89+
echo "[CasaOS-Main] Starting main server as UID:GID $PUID:$PGID..."
90+
echo "[CasaOS-Main] Main service ready - filtering logs for clarity"
91+
touch /var/run/casaos-status/main-started
92+
exec gosu "$PUID:$PGID" /usr/local/bin/casaos-main 2>&1 | filter_logs "main"

0 commit comments

Comments
 (0)