start-two-nodes-in-docker.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. ## This script takes the first argument as docker iamge name,
  4. ## starts two containers running with the built code mount
  5. ## into docker containers.
  6. ##
  7. ## NOTE: containers are not instructed to rebuild emqx,
  8. ## Please use a docker image which is compatible with
  9. ## the docker host.
  10. ##
  11. ## EMQX can only start with longname (https://erlang.org/doc/reference_manual/distributed.html)
  12. ## The host name part of EMQX's node name has to be static, this means we should either
  13. ## pre-assign static IP for containers, or ensure containers can communiate with each other by name
  14. ## this is why a docker network is created, and the containers's names have a dot.
  15. # ensure dir
  16. cd -P -- "$(dirname -- "$0")/.."
  17. IMAGE="${1}"
  18. PROJ_DIR="$(pwd)"
  19. NET='emqx.io'
  20. NODE1="node1.$NET"
  21. NODE2="node2.$NET"
  22. COOKIE='this-is-a-secret'
  23. ## clean up
  24. docker rm -f "$NODE1" >/dev/null 2>&1 || true
  25. docker rm -f "$NODE2" >/dev/null 2>&1 || true
  26. docker network rm "$NET" >/dev/null 2>&1 || true
  27. docker network create "$NET"
  28. docker run -d -t --restart=always --name "$NODE1" \
  29. --net "$NET" \
  30. -e EMQX_NODE_NAME="emqx@$NODE1" \
  31. -e EMQX_NODE_COOKIE="$COOKIE" \
  32. -e WAIT_FOR_ERLANG=60 \
  33. -p 18083:18083 \
  34. -v "$PROJ_DIR"/_build/emqx/rel/emqx:/built \
  35. "$IMAGE" sh -c 'cp -r /built /emqx && /emqx/bin/emqx console'
  36. docker run -d -t --restart=always --name "$NODE2" \
  37. --net "$NET" \
  38. -e EMQX_NODE_NAME="emqx@$NODE2" \
  39. -e EMQX_NODE_COOKIE="$COOKIE" \
  40. -e WAIT_FOR_ERLANG=60 \
  41. -p 18084:18083 \
  42. -v "$PROJ_DIR"/_build/emqx/rel/emqx:/built \
  43. "$IMAGE" sh -c 'cp -r /built /emqx && /emqx/bin/emqx console'
  44. wait (){
  45. container="$1"
  46. while ! docker exec "$container" /emqx/bin/emqx_ctl status >/dev/null 2>&1; do
  47. echo -n '.'
  48. sleep 1
  49. done
  50. }
  51. wait $NODE1
  52. wait $NODE2
  53. echo
  54. docker exec $NODE1 /emqx/bin/emqx_ctl cluster join "emqx@$NODE2"