Skip to main content

Serve Django Media Files

Media files are user-uploaded content such as images, documents, and videos. Unlike static files, media files are generated at runtime and need persistent storage that survives deployments.

The Problem

By default, Docker containers have an ephemeral filesystem. Any files written inside a container are lost when the container is restarted or redeployed. This means user uploads will disappear after every deployment.

Solution 1: Use Appliku Volumes

Appliku volumes provide persistent storage that is mounted into your container and survives deployments.

Step 1: Create a Volume

Go to your application's Volumes tab and create a new volume:

  • Mount path: /app/media
  • Enable Nginx checkbox to serve files directly through Nginx (recommended for performance)

Step 2: Configure Django Settings

# settings.py
import os

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
tip

When you enable the Nginx checkbox in volume settings, Nginx will serve media files directly without passing requests through your Django application. This significantly improves performance for file downloads.

Step 3: Deploy

Save your settings and deploy. Uploaded files will now persist in the mounted volume.

Solution 2: Use S3-Compatible Storage

For applications that need to scale across multiple servers or want offsite storage, use S3-compatible object storage with django-storages.

Step 1: Install Dependencies

Add to your requirements.txt:

django-storages
boto3

Step 2: Configure Settings

# settings.py
import os

DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"

AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME")
AWS_S3_REGION_NAME = os.environ.get("AWS_S3_REGION_NAME", "us-east-1")

Step 3: Set Environment Variables

Add the AWS credentials and bucket name in your application's Environment Variables tab.

note

S3-compatible storage works with any provider that implements the S3 API, including DigitalOcean Spaces, MinIO, and Backblaze B2. Adjust the endpoint URL accordingly.

Which Solution Should I Use?

CriteriaVolumesS3 Storage
Single serverGreat fitWorks
Multiple serversNot recommendedGreat fit
CostIncluded with serverAdditional cost
CDN supportManual setupBuilt-in with most providers
BackupManualProvider-managed