docker: add wait-for-mqtt entrypoint; remove compose override

This commit is contained in:
2025-12-28 15:17:28 +01:00
parent 0135924f7f
commit 79fcf7389f
3 changed files with 28 additions and 4 deletions

View File

@@ -11,7 +11,9 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
# Final image
FROM alpine:3.18
RUN apk add --no-cache ca-certificates
RUN apk add --no-cache ca-certificates netcat-openbsd
COPY --from=builder /app/server /usr/local/bin/server
COPY scripts/wait-for-mqtt.sh /usr/local/bin/wait-for-mqtt.sh
RUN chmod +x /usr/local/bin/wait-for-mqtt.sh
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/server"]
ENTRYPOINT ["/usr/local/bin/wait-for-mqtt.sh"]

22
scripts/wait-for-mqtt.sh Normal file
View File

@@ -0,0 +1,22 @@
#!/bin/sh
# wait-for-mqtt.sh: wait for MQTT broker from $MQTT_BROKER (default tcp://mosquitto:1883)
set -e
BROKER=${MQTT_BROKER:-tcp://mosquitto:1883}
# strip tcp:// if present
BROKER=${BROKER#tcp://}
HOST=${BROKER%%:*}
PORT=${BROKER#*:}
if [ "$HOST" = "$PORT" ]; then
# no port provided
PORT=1883
fi
echo "Waiting for MQTT broker $HOST:$PORT..."
while ! nc -z "$HOST" "$PORT"; do
echo "broker not ready, retrying..."
sleep 1
done
echo "Broker $HOST:$PORT is up, starting server"
exec /usr/local/bin/server

View File

@@ -16,8 +16,8 @@ services:
- MQTT_TOPIC=lambdaiot
depends_on:
- mosquitto
# ensure mosquitto is reachable before launching the server
command: sh -c "apk add --no-cache netcat-openbsd >/dev/null 2>&1 || true; until nc -z mosquitto 1883; do echo waiting for mosquitto...; sleep 1; done; /usr/local/bin/server"
# server image now waits for MQTT broker itself via entrypoint
# no command override needed
mosquitto:
image: eclipse-mosquitto:2.0