PortGate

Blueprints · Docker & Databases

Production 15 min · Docker

NATS on localhost

Publishes NATS on 127.0.0.1. Add accounts before any other host is allowed to connect.

Why this should not be public

A NATS server without accounts lets any client subscribe to every subject and publish to it, which means reading other services' messages and injecting commands into them.

Live config

Baked-in protections

Step 1 · yaml

Compose file

/opt/nats-loopback/compose.yaml

Written under the compose directory. It is not started. The image tag is the release this blueprint was checked with: look at the project's release notes and use the newest patched tag.

services:
  app:
    image: nats:2.10
    ports:
      - "127.0.0.1:4222:4222"
    security_opt:
      - no-new-privileges:true
    restart: unless-stopped

Step 2 · bash

Start script

/opt/nats-loopback/up.sh

Review the image tag and the password, then run this yourself.

#!/bin/sh
set -eu
cd "/opt/nats-loopback"
if grep -q 'change-me' compose.yaml; then
  echo "Replace change-me before starting."
  exit 1
fi
docker compose up -d

Step 3 · bash

Read-only check

/root/portgate-blueprints/nats-loopback-check.sh

Prints status. It does not change the service.

#!/bin/sh
set -eu
if command -v docker >/dev/null 2>&1; then
  docker ps --format '{{.Names}} {{.Ports}}' || echo "Docker is installed but the daemon did not answer."
else
  echo "Docker is not installed. The compose file is staged only."
fi