# Authentik + PostgreSQL + Redis

> Identity provider with SSO, LDAP, and SCIM support.

- Category: Security & Networking
- Difficulty: advanced
- Components: authentik-server, authentik-worker, postgresql, redis
- Tags: authentik, sso, identity, ldap, oauth2, saml
- Source: https://docker.recipes/security/authentik-complete

## Overview

Authentik is an open-source identity provider that focuses on flexibility and versatility, offering comprehensive authentication and authorization capabilities through multiple protocols including OIDC, SAML, OAuth2, and LDAP. Built with modern web technologies, Authentik provides flow-based authentication, multi-factor authentication, user management, and application proxy functionality, making it an excellent alternative to enterprise solutions like Okta or Azure AD for self-hosted environments.

This deployment creates a distributed Authentik infrastructure with four specialized services: authentik-server handles the web interface and API endpoints on ports 9000 (HTTP) and 9443 (HTTPS), authentik-worker manages background tasks and integrations with Docker socket access, postgres provides the primary database for storing users, applications, and configuration data, and redis serves as the cache layer and message broker for inter-service communication. The architecture separates concerns effectively, allowing the worker to handle intensive operations while the server focuses on user interactions.

This stack is ideal for organizations implementing single sign-on across multiple applications, development teams requiring centralized authentication for their services, and system administrators managing user access in hybrid environments. The combination provides enterprise-grade identity management capabilities with the flexibility to customize authentication flows, integrate with existing LDAP directories, and proxy legacy applications that lack modern authentication support.

## Features

- Multi-protocol authentication support including OIDC, SAML, OAuth2, and LDAP with custom flow configuration
- Application proxy functionality for protecting legacy applications without built-in authentication
- Comprehensive multi-factor authentication with TOTP, WebAuthn, SMS, and email verification methods
- SCIM provisioning for automated user lifecycle management with external systems
- Flow-based authentication designer for creating custom login, registration, and recovery processes
- Distributed architecture with dedicated worker processes for background tasks and integrations
- Built-in LDAP outpost for serving as an LDAP provider to legacy applications
- Customizable branding and theming with template override support for white-label deployments

## Use cases

- Enterprise SSO implementation replacing expensive cloud identity providers with self-hosted solution
- Development team authentication hub providing centralized access to GitLab, Jenkins, Grafana, and custom applications
- Homelab identity management for securing self-hosted services like Nextcloud, Plex, and monitoring tools
- Legacy application modernization by adding modern authentication without code changes
- Multi-tenant SaaS platforms requiring isolated authentication flows for different customer organizations
- Educational institutions needing LDAP compatibility for existing systems while adding modern SSO capabilities
- Startup identity infrastructure providing scalable authentication from day one through enterprise growth

## Prerequisites

- Docker host with minimum 2GB RAM and 10GB storage for PostgreSQL data and media files
- Available ports 9000 and 9443 for Authentik web interface and API access
- Strong SECRET_KEY environment variable (minimum 32 characters) for cryptographic operations
- PostgreSQL credentials configured via POSTGRES_USER and POSTGRES_PASSWORD environment variables
- Basic understanding of OAuth2/OIDC flows for application integration configuration
- SSL certificate management knowledge for production HTTPS deployment on port 9443

## docker-compose.yml

```yaml
services:
  authentik-server:
    image: ghcr.io/goauthentik/server:latest
    command: server
    environment:
      - AUTHENTIK_REDIS__HOST=redis
      - AUTHENTIK_POSTGRESQL__HOST=postgres
      - AUTHENTIK_POSTGRESQL__USER=${POSTGRES_USER}
      - AUTHENTIK_POSTGRESQL__PASSWORD=${POSTGRES_PASSWORD}
      - AUTHENTIK_POSTGRESQL__NAME=authentik
      - AUTHENTIK_SECRET_KEY=${SECRET_KEY}
    volumes:
      - authentik-media:/media
      - authentik-custom-templates:/templates
    ports:
      - "9000:9000"
      - "9443:9443"
    depends_on:
      - postgres
      - redis
    networks:
      - authentik-network
    restart: unless-stopped

  authentik-worker:
    image: ghcr.io/goauthentik/server:latest
    command: worker
    environment:
      - AUTHENTIK_REDIS__HOST=redis
      - AUTHENTIK_POSTGRESQL__HOST=postgres
      - AUTHENTIK_POSTGRESQL__USER=${POSTGRES_USER}
      - AUTHENTIK_POSTGRESQL__PASSWORD=${POSTGRES_PASSWORD}
      - AUTHENTIK_POSTGRESQL__NAME=authentik
      - AUTHENTIK_SECRET_KEY=${SECRET_KEY}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - authentik-media:/media
      - authentik-certs:/certs
      - authentik-custom-templates:/templates
    depends_on:
      - postgres
      - redis
    networks:
      - authentik-network
    restart: unless-stopped

  postgres:
    image: postgres:15
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=authentik
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - authentik-network
    restart: unless-stopped

  redis:
    image: redis:alpine
    volumes:
      - redis-data:/data
    networks:
      - authentik-network
    restart: unless-stopped

volumes:
  authentik-media:
  authentik-custom-templates:
  authentik-certs:
  postgres-data:
  redis-data:

networks:
  authentik-network:
    driver: bridge
```

## .env

```bash
# Authentik
POSTGRES_USER=authentik
POSTGRES_PASSWORD=secure_postgres_password
SECRET_KEY=your-very-long-secret-key-generate-with-openssl

# Generate secret: openssl rand -base64 60
# Setup: docker exec -it authentik-server ak setup
```

## Usage notes

- Web UI at http://localhost:9000
- Initial setup wizard
- LDAP outpost available
- Proxy provider for apps
- SAML and OAuth2/OIDC support

## Troubleshooting

- authentik-server fails to start with database connection error: Verify postgres service is healthy and POSTGRES_* environment variables match between services
- Worker tasks not processing with Redis connection timeout: Check redis service status and ensure AUTHENTIK_REDIS__HOST points to correct service name
- Initial setup wizard shows 'Invalid SECRET_KEY' error: Generate new SECRET_KEY with at least 32 random characters and restart all authentik services
- LDAP outpost connection refused: Ensure authentik-worker has proper Docker socket access and outpost configuration matches network settings
- Application proxy returns 502 errors: Verify target application URLs are accessible from authentik-server container and proxy provider configuration is correct
- User authentication flows fail silently: Check authentik-worker logs for background task errors and verify flow stage configuration in admin interface

