Skip to content

Commit bc52aa4

Browse files
fix: auto-generate SECRET_KEY_BASE and improve Postgres startup reliability
Remove the hardcoded placeholder SECRET_KEY_BASE from docker-compose.yml and add an entrypoint script that generates a random one via openssl if not explicitly set. The entrypoint also retries migrations with backoff when Postgres isn't fully ready, preventing the cascade of errors that made the control panel unusable on fresh docker-compose up.
1 parent 09eee20 commit bc52aa4

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ ENV PHX_SERVER=true
6565
# Copy the release from the build stage
6666
COPY --from=builder /app/_build/prod/rel/zentinel_cp ./
6767

68+
# Copy entrypoint script
69+
COPY entrypoint.sh /app/entrypoint.sh
70+
6871
# Create a non-root user
6972
RUN groupadd -r zentinel && useradd -r -g zentinel zentinel
70-
RUN chown -R zentinel:zentinel /app
73+
RUN chmod +x /app/entrypoint.sh && chown -R zentinel:zentinel /app
7174
USER zentinel
7275

7376
EXPOSE 4000
7477

7578
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
7679
CMD curl -f http://localhost:4000/health || exit 1
7780

78-
CMD /app/bin/zentinel_cp eval "ZentinelCp.Release.migrate()" && \
79-
/app/bin/zentinel_cp eval "ZentinelCp.Release.seed()" && \
80-
/app/bin/zentinel_cp start
81+
ENTRYPOINT ["/app/entrypoint.sh"]

docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ services:
55
- "4000:4000"
66
environment:
77
DATABASE_URL: "ecto://zentinel:zentinel@postgres:5432/zentinel_cp"
8-
SECRET_KEY_BASE: "change-me-in-production-use-mix-phx-gen-secret-to-generate"
8+
# SECRET_KEY_BASE is auto-generated if not set. To persist sessions across
9+
# restarts, set it explicitly: openssl rand -base64 48
910
# PHX_HOST: "your-domain.com" # Set to your server's domain or IP
1011
PORT: "4000"
1112
S3_ENDPOINT: "http://minio:9000"
@@ -24,7 +25,7 @@ services:
2425
interval: 30s
2526
timeout: 5s
2627
retries: 3
27-
start_period: 15s
28+
start_period: 60s
2829

2930
postgres:
3031
image: postgres:17-alpine

entrypoint.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Generate a random SECRET_KEY_BASE if not set
5+
if [ -z "$SECRET_KEY_BASE" ]; then
6+
export SECRET_KEY_BASE=$(openssl rand -base64 48)
7+
echo "[entrypoint] Generated random SECRET_KEY_BASE"
8+
fi
9+
10+
# Wait for the database to accept connections (belt-and-suspenders on top of
11+
# Docker healthchecks — pg_isready can return 0 before the server is fully
12+
# ready to handle queries).
13+
if [ -n "$DATABASE_URL" ]; then
14+
echo "[entrypoint] Waiting for database..."
15+
retries=0
16+
max_retries=30
17+
until /app/bin/zentinel_cp eval "ZentinelCp.Release.migrate()" 2>/dev/null; do
18+
retries=$((retries + 1))
19+
if [ "$retries" -ge "$max_retries" ]; then
20+
echo "[entrypoint] Database not ready after ${max_retries} attempts, running migration anyway..."
21+
/app/bin/zentinel_cp eval "ZentinelCp.Release.migrate()"
22+
break
23+
fi
24+
echo "[entrypoint] Database not ready (attempt ${retries}/${max_retries}), retrying in 2s..."
25+
sleep 2
26+
done
27+
echo "[entrypoint] Database migrations complete."
28+
else
29+
echo "[entrypoint] Running database migrations..."
30+
/app/bin/zentinel_cp eval "ZentinelCp.Release.migrate()"
31+
fi
32+
33+
# Seed default data
34+
echo "[entrypoint] Seeding database..."
35+
/app/bin/zentinel_cp eval "ZentinelCp.Release.seed()"
36+
37+
# Start the application
38+
echo "[entrypoint] Starting Zentinel Control Plane..."
39+
exec /app/bin/zentinel_cp start

0 commit comments

Comments
 (0)