Skip to content

Instantly share code, notes, and snippets.

@OliveiraCleidson
Created August 26, 2025 16:36
Show Gist options
  • Select an option

  • Save OliveiraCleidson/06913456338909e8e459240108a3a273 to your computer and use it in GitHub Desktop.

Select an option

Save OliveiraCleidson/06913456338909e8e459240108a3a273 to your computer and use it in GitHub Desktop.
Create a NATS Cluster with JetStream using Docker Compose

Testing

Create a docker-compose.yml file and place the NATS configuration files in the same folder. Then run:

docker compose up -d

To test the NATS server, run:

docker run --network devsetup --rm -it natsio/nats-box

After this command, your terminal will be inside the container. Run the following command to connect to the NATS server:

nats sub -s nats://nats-1:4222 hello &

A successful subscription will return something like:

16:27:54 Subscribing on hello 

Now publish a message to servers 2 and 3:

nats pub -s "nats://nats-2:4222" hello first

# message that should published
16:29:04 Published 5 bytes to "hello"

# message of the subscription of line 16 received the message
[#1] Received on "hello"
first

# Test server 3
nats pub -s "nats://nats-3:4222" hello second

# message that should published
16:30:01 Published 6 bytes to "hello"

# message of the subscription of line 16 received the message
[#3] Received on "hello"
second

Testing Reconnection

To simulate a node failure, in another terminal run:

docker compose down nats-1

In the NATS box, you will see a message like:

16:31:52 Disconnected due to: EOF, will attempt reconnect
16:31:52 Reconnected [nats://172.19.0.4:4222]

Notice that in line 16 the subscription was originally connected to nats-1. When you stop that server, the NATS client automatically reconnects to another node in the cluster.

Done!

version: "3.9"
name: dev-setup
services:
nats-1:
image: nats:latest
container_name: ds-nats-server-1
ports:
- "8222:8222"
command: "-c /etc/nats/nats-server.conf --http_port 8222"
networks:
- devsetup
volumes:
# Volume bind of storage (optional)
# - '/var/lib/dev-setup/nats/server-1:/nats/storage'
- './nats-server-1.conf:/etc/nats/nats-server.conf'
nats-2:
image: nats:latest
container_name: ds-nats-server-2
command: "-c /etc/nats/nats-server.conf --http_port 8222"
networks:
- devsetup
volumes:
# Volume bind of storage (optional)
# - '/var/lib/dev-setup/nats/server-2:/nats/storage'
- './nats-server-2.conf:/etc/nats/nats-server.conf'
nats-3:
image: nats:latest
container_name: ds-nats-server-3
command: "-c /etc/nats/nats-server.conf --http_port 8222"
networks:
- devsetup
# depends_on: ["nats-2"]
volumes:
# Volume bind of storage (optional)
# - '/var/lib/dev-setup/nats/server-3:/nats/storage'
- './nats-server-3.conf:/etc/nats/nats-server.conf'
networks:
devsetup:
name: devsetup
server_name=n1-c1
listen=4222
jetstream {
store_dir=/nats/storage
}
cluster {
name: C1
listen: 0.0.0.0:6222
routes: [
nats://ruser:T0pS3cr3t@nats-2:6222
nats://ruser:T0pS3cr3t@nats-3:6222
]
}
server_name=n2-c1
listen=4222
jetstream {
store_dir=/nats/storage
}
cluster {
name: C1
listen: 0.0.0.0:6222
routes: [
nats://ruser:T0pS3cr3t@nats-1:6222
nats://ruser:T0pS3cr3t@nats-3:6222
]
}
server_name=n3-c1
listen=4222
jetstream {
store_dir=/nats/storage
}
cluster {
name: C1
listen: 0.0.0.0:6222
routes: [
nats://ruser:T0pS3cr3t@nats-1:6222
nats://ruser:T0pS3cr3t@nats-2:6222
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment