01Why Self-Host Nextcloud?
02Understanding the Stack
Always use separate containers for different services rather than cramming everything into one container. This makes updates, scaling, and troubleshooting much easier.
03Docker Compose Configuration
1version: '3.8'23services: 4 nextcloud-db: 5 image: postgres:15-alpine6 restart: unless-stopped7 volumes: 8 - nextcloud_db:/var/lib/postgresql/data9 environment: 10 - POSTGRES_DB=nextcloud11 - POSTGRES_USER=nextcloud12 - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}13 networks: 14 - nextcloud-network1516 nextcloud-redis: 17 image: redis:7-alpine18 restart: unless-stopped19 command: redis-server --requirepass ${REDIS_PASSWORD}20 networks: 21 - nextcloud-network2223 nextcloud-app: 24 image: nextcloud:28-apache25 restart: unless-stopped26 ports: 27 - "8080:80"28 volumes: 29 - nextcloud_data:/var/www/html30 - ./data:/var/www/html/data31 - ./config:/var/www/html/config32 - ./apps:/var/www/html/custom_apps33 environment: 34 - POSTGRES_HOST=nextcloud-db35 - POSTGRES_DB=nextcloud36 - POSTGRES_USER=nextcloud37 - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}38 - REDIS_HOST=nextcloud-redis39 - REDIS_HOST_PASSWORD=${REDIS_PASSWORD}40 - NEXTCLOUD_ADMIN_USER=${ADMIN_USER}41 - NEXTCLOUD_ADMIN_PASSWORD=${ADMIN_PASSWORD}42 - NEXTCLOUD_TRUSTED_DOMAINS=${TRUSTED_DOMAINS}43 - APACHE_DISABLE_REWRITE_IP=144 depends_on: 45 - nextcloud-db46 - nextcloud-redis47 networks: 48 - nextcloud-network4950 nextcloud-cron: 51 image: nextcloud:28-apache52 restart: unless-stopped53 volumes: 54 - nextcloud_data:/var/www/html55 - ./data:/var/www/html/data56 - ./config:/var/www/html/config57 - ./apps:/var/www/html/custom_apps58 entrypoint: /cron.sh59 depends_on: 60 - nextcloud-db61 - nextcloud-redis62 networks: 63 - nextcloud-network6465volumes: 66 nextcloud_db: 67 nextcloud_data: 6869networks: 70 nextcloud-network: 71 driver: bridge04Environment Variables and Security
1# Database Configuration2POSTGRES_PASSWORD=your-secure-database-password-here34# Redis Configuration 5REDIS_PASSWORD=your-secure-redis-password-here67# Nextcloud Admin Account8ADMIN_USER=admin9ADMIN_PASSWORD=your-secure-admin-password-here1011# Domain Configuration12TRUSTED_DOMAINS=nextcloud.yourdomain.com localhostNever commit your .env file to version control. Add it to your .gitignore file immediately. Use strong, unique passwords for all services.
05First-Time Deployment
1# Create directory structure2mkdir -p data config apps34# Set proper permissions (adjust UID/GID as needed)5sudo chown -R 33:33 data config apps67# Start the services8docker compose up -d910# Monitor the initialization process11docker compose logs -f nextcloud-appThe first startup can take 5-10 minutes. Don't panic if it seems slow – Nextcloud is setting up the database schema and installing default apps.
06Performance and Configuration Tuning
07Ongoing Maintenance and Updates
Always test updates in a staging environment first. While Nextcloud updates are generally smooth, complex installations with many apps can sometimes have issues.