Skip to content

Instantly share code, notes, and snippets.

@pySilver
Forked from wallyqs/readme.md
Created August 14, 2024 22:57
Show Gist options
  • Save pySilver/935871f87671cf67e3b51d2dbe17138d to your computer and use it in GitHub Desktop.
Save pySilver/935871f87671cf67e3b51d2dbe17138d to your computer and use it in GitHub Desktop.
JetStream on Docker with Docker Compose

First, create a few volumes so that state is persisted across restarts:

docker volume create nats1
docker volume create nats2
docker volume create nats3

Now create a docker-compose.yaml with the following:

version: '3.9'
services:
  nats1:
    image: docker.io/nats:2.9.20
    ports:
      - "4222:4222"
      - "8222:8222"      
    volumes:
      - nats1:/data
    command:
      - "--name=nats1"
      - "--cluster_name=c1"
      - "--cluster=nats://nats1:6222"
      - "--routes=nats-route://nats1:6222,nats-route://nats2:6222,nats-route://nats3:6222"
      - "--http_port=8222"
      - "--js"
      - "--sd=/data"

  nats2:
    image: docker.io/nats:2.9.20
    ports:
      - "4223:4222"
      - "8223:8222"
    volumes:
      - nats2:/data
    command:
      - "--name=nats2"
      - "--cluster_name=c1"
      - "--cluster=nats://nats2:6222"
      - "--routes=nats-route://nats1:6222,nats-route://nats2:6222,nats-route://nats3:6222"
      - "--http_port=8222"
      - "--js"
      - "--sd=/data"

  nats3:
    image: docker.io/nats:2.9.20
    ports:
      - "4224:4222"
      - "8224:8222"
    volumes:
      - nats3:/data
    command:
      - "--name=nats3"
      - "--cluster_name=c1"
      - "--cluster=nats://nats3:6222"
      - "--routes=nats-route://nats1:6222,nats-route://nats2:6222,nats-route://nats3:6222"
      - "--http_port=8222"
      - "--js"
      - "--sd=/data"

volumes:
  nats1:
    external: true
  nats2:
    external: true
  nats3:
    external: true

Start the cluster:

docker-compose up 

It should be possible to target the cluster locally:

nats -s localhost:4222 stream ls
No Streams defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment