The NATS server is provided as a Docker image on that you can run using the Docker daemon. The NATS server Docker image is extremely lightweight, coming in under 10 MB in size.
actively maintains and supports the NATS server Docker image.
Nightly
The nightly build container can be found
Usage
To use the Docker container image, install Docker and pull the public image:
docker pull nats
Run the NATS server image:
docker run nats
By default the NATS server exposes multiple ports:
4222 is for clients.
8222 is an HTTP management port for information reporting.
6222 is a routing port for clustering.
The default ports may be customized by providing either a -p or -P option on the docker run command line.
The following steps illustrate how to run a server with the ports exposed on a docker network.
It is also straightforward to create a cluster using Docker Compose. Below is a simple example that uses a network named 'nats' to create a full mesh cluster.
Now, the following should work: make a subscription on one of the nodes and publish it from another node. You should be able to receive the message without problems.
docker run --network nats --rm -it natsio/nats-box
Inside the container:
nats sub -s nats://nats:4222 hello &
nats pub -s "nats://nats-1:4222" hello first
nats pub -s "nats://nats-2:4222" hello second
Stopping the seed node, which received the subscription, should trigger an automatic failover to the other nodes:
docker-compose -f nats-cluster.yaml stop nats
Output extract:
...
16e55f1c4f3c:~# 10:47:28 Disconnected due to: EOF, will attempt reconnect
10:47:28 Disconnected due to: EOF, will attempt reconnect
10:47:28 Reconnected [nats://172.18.0.4:4222]
Publishing again will continue to work after the reconnection:
nats pub -s "nats://nats-1:4222" hello again
nats pub -s "nats://nats-2:4222" hello again
Tutorial
NOTE Since the Docker image protects routes using credentials we need to provide them above. Extracted
See the for more instructions on using the NATS server Docker image.