Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Created July 15, 2023 04:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wallyqs/5378f5abcbe4b1b683268cacf2b672d3 to your computer and use it in GitHub Desktop.
Save wallyqs/5378f5abcbe4b1b683268cacf2b672d3 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