Home / Clayi CodeTemplates / Templates / Production Docker Compose Boilerplate for Next.js, Postgres & Redis

Production Docker Compose Boilerplate for Next.js, Postgres & Redis

Production Docker Compose Boilerplate for Next.js, Postgres & Redis
  • Category Templates
  • Type YML
  • Platform Cross-platform
  • Language YAML
  • Price Free
  • Views 1 249
  • Comments 0
View Resource
Production Docker Compose Boilerplate for Next.js, Postgres & Redis

The Power of Container Orchestration with Docker Compose

Deploying a modern, full-stack web application into a live production environment requires coordinating multiple complex services, such as a frontend framework, a relational database, an in-memory cache, and a secure web server. Manually installing and linking these components on a raw Linux machine is highly error-prone. This "Production Docker Compose Boilerplate for Next.js, Postgres & Redis" elegantly solves this problem. It allows developers to define their entire multi-container infrastructure inside a single, version-controlled YAML file, spinning up an entire production-ready ecosystem with a single command.

Building the Next.js Frontend Application Service

The core of this architecture is the web service, which represents the Next.js frontend application. Instead of pulling a pre-built image, the configuration uses build: context: . to dynamically compile your custom Next.js application from a local Dockerfile during deployment. By assigning a clear container_name: clayi_nextjs_app and setting the restart: always policy, you guarantee that if the Node.js server unexpectedly crashes due to an unhandled exception, the Docker daemon will instantly and automatically reboot the container, minimizing application downtime.

Configuring Environment Variables for Seamless Integration

Modern applications rely heavily on environment variables to connect to external services. The boilerplate effortlessly passes critical configuration data—such as the NODE_ENV: production flag and the crucial DATABASE_URL—directly into the Next.js container. Because all of these containers operate on the same custom app-network, you don't need to use complex internal IP addresses. Instead, the Next.js app can seamlessly connect to the database and cache simply by using their service names (db and redis) natively within the connection strings.

Deploying a Robust PostgreSQL Database Engine

For data persistence, this boilerplate utilizes the official postgres:16-alpine image. The Alpine Linux variant is chosen specifically because it is incredibly lightweight, minimizing the container's attack surface and downloading rapidly. The configuration securely injects the required POSTGRES_USER and POSTGRES_PASSWORD environment variables to initialize the database securely on first boot. Furthermore, the volumes directive creates a named volume (postgres_data), physically mapping the internal database files to the host machine, ensuring your data is never lost if the container is destroyed.

Ensuring Database Reliability with Docker Healthchecks

One of the most common issues in multi-container setups is the frontend application attempting to connect to the database before the database engine has actually finished booting up, leading to fatal connection timeouts. This script solves this beautifully by implementing a strict Docker healthcheck on the PostgreSQL container. It periodically runs the native pg_isready command. Because the web service is configured to depend on the db service specifically via the condition: service_healthy flag, the Next.js app will patiently wait in a suspended state until the database is 100% ready to accept connections.

Implementing the Redis Cache Engine for High Performance

To ensure your Next.js application scales smoothly under heavy traffic, caching is absolutely mandatory. The boilerplate incorporates a dedicated redis:7-alpine container for exactly this purpose. The custom command redis-server --save 60 1 --loglevel notice instructs the Redis engine to periodically save its in-memory cache to the disk every 60 seconds if at least one key changes, and the redis_data volume ensures these snapshots are permanently preserved. This configuration provides lightning-fast data retrieval for your frontend while maintaining strict data persistence.

Routing Traffic Securely with an Nginx Reverse Proxy

Exposing a Node.js server directly to the public internet is generally considered bad practice. Instead, this stack utilizes an nginx:alpine container acting as a highly secure reverse proxy. This proxy sits on the front lines, exclusively intercepting all external traffic on ports 80 and 443. By mapping read-only configuration volumes (nginx.conf and certbot/conf), the Nginx container effortlessly routes clean HTTP traffic back to the Next.js application while completely handling the heavy computational lifting required for SSL/TLS certificate encryption and decryption.

Download the Complete Production Docker Compose Boilerplate

Engineering a flawless, interconnected container architecture from scratch requires deep DevOps knowledge and hours of tedious YAML syntax debugging. To streamline your deployment pipeline immediately, you can download this complete "Production Docker Compose Boilerplate" directly from this page. By copying this file into your project repository, you can instantly spin up a robust, scalable, and highly secure web infrastructure, allowing your development team to focus entirely on writing brilliant Next.js code rather than fighting with complex server configurations.

Popularity
0%
  • Votes: 9
  • Comments: 0

Help

Can't download assets? How to use Clayi Assets Assets not working? Can't copy? How to use Clayi Code Snippet not working?

Free Production Docker Compose Boilerplate for Next.js, Postgres & Redis YML Download

# ===============================================================================
# Clayi Assets - Production Docker Compose Boilerplate
# Stack: Next.js + PostgreSQL 16 + Redis 7 + Nginx Reverse Proxy
# License: MIT License
# ===============================================================================
version: '3.8'

services:
  # Next.js Web Frontend Application
  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: clayi_nextjs_app
    restart: always
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://clayi_user:super_secret_password@db:5432/clayi_db?schema=public
      REDIS_URL: redis://redis:6379
    ports:
      - "3000:3000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - app-network

  # PostgreSQL Database Engine
  db:
    image: postgres:16-alpine
    container_name: clayi_postgres_db
    restart: always
    environment:
      POSTGRES_USER: clayi_user
      POSTGRES_PASSWORD: super_secret_password
      POSTGRES_DB: clayi_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U clayi_user -d clayi_db"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis Cache Engine
  redis:
    image: redis:7-alpine
    container_name: clayi_redis_cache
    restart: always
    command: redis-server --save 60 1 --loglevel notice
    volumes:
      - redis_data:/data
    networks:
      - app-network

  # Nginx Reverse Proxy & SSL Router
  proxy:
    image: nginx:alpine
    container_name: clayi_nginx_proxy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./certbot/conf:/etc/letsencrypt:ro
    depends_on:
      - web
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  postgres_data:
  redis_data:

Download Assets
Wait 10 sec
Free file download — fast & secure!
Download this open-source asset for free on Clayi Assets. Direct CDN link after a short wait — no account required.

New Resources

Popular Resources

There are no comments yet :(

Production Docker Compose Boilerplate for Next.js, Postgres & Redis
Tell us what you think about "Production Docker Compose Boilerplate for Next.js, Postgres & Redis"
Information
Users of Guests are not allowed to comment this publication.