docker: add wait-for-mqtt entrypoint; remove compose override
This commit is contained in:
+4
-2
@@ -11,7 +11,9 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
|
|||||||
|
|
||||||
# Final image
|
# Final image
|
||||||
FROM alpine:3.18
|
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 --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
|
EXPOSE 8080
|
||||||
ENTRYPOINT ["/usr/local/bin/server"]
|
ENTRYPOINT ["/usr/local/bin/wait-for-mqtt.sh"]
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -16,8 +16,8 @@ services:
|
|||||||
- MQTT_TOPIC=lambdaiot
|
- MQTT_TOPIC=lambdaiot
|
||||||
depends_on:
|
depends_on:
|
||||||
- mosquitto
|
- mosquitto
|
||||||
# ensure mosquitto is reachable before launching the server
|
# server image now waits for MQTT broker itself via entrypoint
|
||||||
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"
|
# no command override needed
|
||||||
|
|
||||||
mosquitto:
|
mosquitto:
|
||||||
image: eclipse-mosquitto:2.0
|
image: eclipse-mosquitto:2.0
|
||||||
|
|||||||
Reference in New Issue
Block a user