Skip to main content
NATS Server provides official Docker images that make it easy to run NATS in containerized environments. The images are available on Docker Hub and include the server along with useful utilities.

Official Docker Image

The official NATS Server image is available at:
The image includes:
  • nats-server - The NATS Server binary
  • nats - The NATS CLI tool
  • nsc - The NATS Security CLI

Quick Start

Run a basic NATS Server with default settings:
This exposes the client port (4222) on your host machine.

Port Mappings

NATS Server uses several ports for different purposes:

Volume Mounts

Configuration File

Mount a custom configuration file:

Data Persistence

For JetStream deployments, mount a volume for data persistence:

Complete Example

Docker Compose

For more complex deployments, use Docker Compose:
docker-compose.yml
Start the service:

Clustering with Docker Compose

Deploy a 3-node NATS cluster:
docker-compose-cluster.yml

Default Configuration

The Docker image includes a default configuration file at /nats/conf/nats-server.conf:
nats-server.conf

Command Line Flags

You can override configuration using command-line flags:
Command-line flags take precedence over configuration file settings.

JetStream with Docker

Enable JetStream with persistent storage:

Health Checks

Add a health check to your Docker configuration:
Or using curl:

Inspecting the Image

The NATS Docker image is built using a multi-stage Dockerfile that:
  1. Builds NATS Server, NSC, and NATS CLI from source
  2. Creates a minimal Alpine-based image
  3. Exposes ports 4222, 8222, 6222, and 5222
  4. Sets the entrypoint to /bin/nats-server
  5. Includes a default configuration file
View the installed version:

Environment Variables

While NATS Server primarily uses configuration files, you can use environment variables in your configuration:
Then pass them to Docker:

Logging

View container logs:
Follow logs in real-time:

Best Practices

Use Named Volumes

Always use named volumes for JetStream data to ensure persistence across container restarts.

Configure Resource Limits

Set memory and CPU limits to prevent resource exhaustion in production.

Enable Monitoring

Always expose the monitoring port (8222) for health checks and metrics.

Use Configuration Files

Prefer configuration files over command-line flags for complex deployments.

Troubleshooting

Container Won’t Start

Check logs for configuration errors:

Connection Refused

Verify port mappings:

Configuration Not Loading

Ensure the config file path is correct and the file is mounted properly:

Next Steps

Kubernetes Deployment

Learn how to deploy NATS on Kubernetes

Configuration

Explore advanced configuration options