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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. ## This script takes the first argument as docker image 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. -p 18083:18083 \
  33. -v "$PROJ_DIR"/_build/emqx/rel/emqx:/built \
  34. "$IMAGE" sh -c 'cp -r /built /emqx && /emqx/bin/emqx console'
  35. docker run -d -t --restart=always --name "$NODE2" \
  36. --net "$NET" \
  37. -e EMQX_NODE_NAME="emqx@$NODE2" \
  38. -e EMQX_NODE_COOKIE="$COOKIE" \
  39. -p 18084:18083 \
  40. -v "$PROJ_DIR"/_build/emqx/rel/emqx:/built \
  41. "$IMAGE" sh -c 'cp -r /built /emqx && /emqx/bin/emqx console'
  42. wait (){
  43. container="$1"
  44. while ! docker exec "$container" /emqx/bin/emqx_ctl status >/dev/null 2>&1; do
  45. echo -n '.'
  46. sleep 1
  47. done
  48. }
  49. wait $NODE1
  50. wait $NODE2
  51. echo
  52. docker exec $NODE1 /emqx/bin/emqx_ctl cluster join "emqx@$NODE2"