Official Docker Image
The official NATS Server image is available at:nats-server- The NATS Server binarynats- The NATS CLI toolnsc- The NATS Security CLI
Quick Start
Run a basic NATS Server with default settings: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
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:Inspecting the Image
The NATS Docker image is built using a multi-stage Dockerfile that:- Builds NATS Server, NSC, and NATS CLI from source
- Creates a minimal Alpine-based image
- Exposes ports 4222, 8222, 6222, and 5222
- Sets the entrypoint to
/bin/nats-server - Includes a default configuration file
Environment Variables
While NATS Server primarily uses configuration files, you can use environment variables in your configuration:Logging
View container logs: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