Deploy with Docker

This chapter introduces how to use the official Docker image to install and run EMQX, and how to use Docker Compose to build an EMQX cluster.

Note

  1. If you want to keep the data, mount the EMQX data directory (/opt/emqx/data) out of the container, so that the data will persist even if the container no longer exists.

  2. In Docker, localhost or 127.0.0.1 points to the internal address of the container. Use the host’s IP or host networkingDeploy with Docker - 图1 (opens new window) to access the host address. If you are using Docker for Mac or Docker for Windows, you can use host.docker.internal as the host address.

Use Docker to Run A Single EMQX Node

This section will introduce how to use the Docker image to install the latest version of EMQX. If you want to work with other versions, please visit the EMQX Deployment pageDeploy with Docker - 图2 (opens new window).

  1. To get the Docker image, run:
  1. docker pull emqx/emqx:5.1.0
  1. To start the Docker container, run:
  1. docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx:5.1.0

For more information about EMQX official docker image, see Docker Hub - emqxDeploy with Docker - 图3 (opens new window).

Use Docker Compose to Build an EMQX Cluster

Docker Compose is a tool for defining and running multi-container Docker applications. This section introduces how to use Docker Compose to create a static EMQX cluster.

TIP

Docker Compose is already included in Docker Desktop. If your Docker Compose still needs to be installed, you may refer to Install Docker ComposeDeploy with Docker - 图4 (opens new window) for detailed operating steps.

  1. Create a docker-compose.yml file under any directory with the following content:
  1. version: '3'
  2. services:
  3. emqx1:
  4. image: emqx:5.1.0
  5. container_name: emqx1
  6. environment:
  7. - "EMQX_NODE_NAME=emqx@node1.emqx.io"
  8. - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
  9. - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io]"
  10. healthcheck:
  11. test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
  12. interval: 5s
  13. timeout: 25s
  14. retries: 5
  15. networks:
  16. emqx-bridge:
  17. aliases:
  18. - node1.emqx.io
  19. ports:
  20. - 1883:1883
  21. - 8083:8083
  22. - 8084:8084
  23. - 8883:8883
  24. - 18083:18083
  25. # volumes:
  26. # - $PWD/emqx1_data:/opt/emqx/data
  27. emqx2:
  28. image: emqx:5.1.0
  29. container_name: emqx2
  30. environment:
  31. - "EMQX_NODE_NAME=emqx@node2.emqx.io"
  32. - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
  33. - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io]"
  34. healthcheck:
  35. test: ["CMD", "/opt/emqx/bin/emqx_ctl", "status"]
  36. interval: 5s
  37. timeout: 25s
  38. retries: 5
  39. networks:
  40. emqx-bridge:
  41. aliases:
  42. - node2.emqx.io
  43. # volumes:
  44. # - $PWD/emqx2_data:/opt/emqx/data
  45. networks:
  46. emqx-bridge:
  47. driver: bridge
  1. In the command line tool, switch to the directory where docker-compose.yml is stored, and run the following command to start the EMQX cluster:
  1. docker-compose up -d
  1. To check the cluster status, run:
  1. $ docker exec -it emqx1 sh -c "emqx_ctl cluster status"
  2. Cluster status: #{running_nodes => ['emqx@node1.emqx.com','emqx@node2.emqx.com'],
  3. stopped_nodes => []}

Next

Use an MQTT client to connect EMQX for message publish/subscribe. For more information, see Publish and Subscribe.

  • On how to configure EMQX parameters and other features, see Configuration.

  • On how to build an EMQX cluster with multiple nodes, see Clustering.