chore(docker): Start emqx in foreground mode in docker container

This commit is contained in:
Zaiming Shi 2021-01-28 19:47:51 +01:00 committed by Rory Z
parent 64e7056f3f
commit 9140eafd94
5 changed files with 6 additions and 73 deletions

View File

@ -17,7 +17,7 @@ services:
- | - |
sed -i "s 127.0.0.1 $$(ip route show |grep "link" |awk '{print $$1}') g" /opt/emqx/etc/acl.conf sed -i "s 127.0.0.1 $$(ip route show |grep "link" |awk '{print $$1}') g" /opt/emqx/etc/acl.conf
sed -i '/emqx_telemetry/d' /opt/emqx/data/loaded_plugins sed -i '/emqx_telemetry/d' /opt/emqx/data/loaded_plugins
/usr/bin/start.sh /opt/emqx/bin/emqx foreground
healthcheck: healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"] test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 5s interval: 5s
@ -44,7 +44,7 @@ services:
- | - |
sed -i "s 127.0.0.1 $$(ip route show |grep "link" |awk '{print $$1}') g" /opt/emqx/etc/acl.conf sed -i "s 127.0.0.1 $$(ip route show |grep "link" |awk '{print $$1}') g" /opt/emqx/etc/acl.conf
sed -i '/emqx_telemetry/d' /opt/emqx/data/loaded_plugins sed -i '/emqx_telemetry/d' /opt/emqx/data/loaded_plugins
/usr/bin/start.sh /opt/emqx/bin/emqx foreground
healthcheck: healthcheck:
test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"] test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
interval: 5s interval: 5s

View File

@ -43,7 +43,7 @@ LABEL org.label-schema.docker.dockerfile="Dockerfile" \
ARG QEMU_ARCH=x86_64 ARG QEMU_ARCH=x86_64
ARG EMQX_NAME=emqx ARG EMQX_NAME=emqx
COPY deploy/docker/docker-entrypoint.sh deploy/docker/start.sh tmp/qemu-$QEMU_ARCH-stati* /usr/bin/ COPY deploy/docker/docker-entrypoint.sh tmp/qemu-$QEMU_ARCH-stati* /usr/bin/
COPY --from=builder /emqx/_build/$EMQX_NAME/rel/emqx /opt/emqx COPY --from=builder /emqx/_build/$EMQX_NAME/rel/emqx /opt/emqx
RUN ln -s /opt/emqx/bin/* /usr/local/bin/ RUN ln -s /opt/emqx/bin/* /usr/local/bin/
@ -77,4 +77,4 @@ EXPOSE 1883 8081 8083 8084 8883 11883 18083 4369 4370 5369 6369 6370
ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"] ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"]
CMD ["/usr/bin/start.sh"] CMD ["/opt/emqx/bin/emqx", "foreground"]

View File

@ -71,7 +71,6 @@ These environment variables will ignore for configuration file.
| ---------------------------| ------------------ | ------------------------- | ------------------------------------- | | ---------------------------| ------------------ | ------------------------- | ------------------------------------- |
| EMQX_NAME | container name | none | emqx node short name | | EMQX_NAME | container name | none | emqx node short name |
| EMQX_HOST | container IP | none | emqx node host, IP or FQDN | | EMQX_HOST | container IP | none | emqx node host, IP or FQDN |
| EMQX_WAIT_TIME | 5 | none | wait time in sec before timeout |
The list is incomplete and may changed with [etc/emqx.conf](https://github.com/emqx/emqx/blob/master/etc/emqx.conf) and plugin configuration files. But the mapping rule is similar. The list is incomplete and may changed with [etc/emqx.conf](https://github.com/emqx/emqx/blob/master/etc/emqx.conf) and plugin configuration files. But the mapping rule is similar.

View File

@ -40,10 +40,6 @@ if [[ -z "$EMQX_HOST" ]]; then
export EMQX_HOST export EMQX_HOST
fi fi
if [[ -z "$EMQX_WAIT_TIME" ]]; then
export EMQX_WAIT_TIME=5
fi
if [[ -z "$EMQX_NODE_NAME" ]]; then if [[ -z "$EMQX_NODE_NAME" ]]; then
export EMQX_NODE_NAME="$EMQX_NAME@$EMQX_HOST" export EMQX_NODE_NAME="$EMQX_NAME@$EMQX_HOST"
fi fi

View File

@ -1,62 +0,0 @@
#!/bin/sh
set -e -u
EMQX_WAIT_TIME=${EMQX_WAIT_TIME:-5}
emqx_exit(){
# At least erlang.log.1 exists
if [ -f /opt/emqx/log/erlang.log.1 ]; then
# tail emqx.log.*
erlang_log=$(echo $(ls -t /opt/emqx/log/erlang.log.*) | awk '{print $1}')
num=$(sed -n -e '/LOGGING STARTED/=' ${erlang_log} | tail -1)
[ ! -z $num ] && [ $num -gt 2 ] && tail -n +$(expr $num - 2) ${erlang_log}
fi
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx exit abnormally"
exit 1
}
## EMQ Main script
# When receiving the EXIT signal, execute emqx_exit function
trap "emqx_exit" EXIT
# Start and run emqx, and when emqx crashed, this container will stop
/opt/emqx/bin/emqx start
# Sleep 5 seconds to wait for the loaded plugins catch up.
sleep 5
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx start"
## Fork tailing erlang.log, the fork is not killed after this script exits
## The assumption is that this is the docker entrypoint,
## hence docker container is terminated after entrypoint exists
tail -f /opt/emqx/log/erlang.log.1 &
# monitor emqx is running, or the docker must stop to let docker PaaS know
# warning: never use infinite loops such as `` while true; do sleep 1000; done`` here
# you must let user know emqx crashed and stop this container,
# and docker dispatching system can known and restart this container.
IDLE_TIME=0
MGMT_CONF='/opt/emqx/etc/plugins/emqx_management.conf'
MGMT_PORT=$(sed -n -r '/^management.listener.http[ \t]=[ \t].*$/p' $MGMT_CONF | sed -r 's/^management.listener.http = (.*)$/\1/g')
while [ $IDLE_TIME -lt 5 ]; do
IDLE_TIME=$(expr $IDLE_TIME + 1)
if curl http://localhost:${MGMT_PORT}/status >/dev/null 2>&1; then
IDLE_TIME=0
# Print the latest erlang.log
now_erlang_log=$(ps -ef |grep "tail -f /opt/emqx/log/erlang.log" |grep -v grep | sed -r "s/.*tail -f (.*)/\1/g")
new_erlang_log="$(ls -t /opt/emqx/log/erlang.log.* | head -1)"
if [ $now_erlang_log != $new_erlang_log ];then
tail -f $new_erlang_log &
kill $(ps -ef |grep "tail -f $now_erlang_log" | grep -v grep | awk '{print $1}')
fi
else
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx not running, waiting for recovery in $((25-IDLE_TIME*5)) seconds"
fi
sleep $EMQX_WAIT_TIME
done
# If running to here (the result 5 times not is running, thus in 25s emqx is not running), exit docker image
# Then the high level PaaS, e.g. docker swarm mode, will know and alert, rebanlance this service